Finding two branches of the nearest common parent node, in the Internet to see a very interesting solution, the original text in the http://www.cnblogs.com/remlostime/archive/2012/11/26/2788795.html
The general idea is to traverse from the child node depth to the root node and then compare the bifurcation points of the two paths. He used the recursion:
1 structNode2 {3 intVal;4Node *Left ;5Node *Right ;6 Node (): Left (null), right (null) {}7 };8 9Node *findfather (node *root, node *node1, node *node2,BOOL&node1find,BOOL&node2find)Ten { One if(Root = =NULL) A returnNULL; - - BOOLLeftnode1find =false, Leftnode2find =false; theNode *leftnode = Findfather (root->Left , Node1, Node2, Leftnode1find, leftnode2find); - if(Leftnode! =NULL) - returnLeftnode; - + BOOLRightnode1find =false, Rightnode2find =false; -Node *rightnode = Findfather (root->Right , Node1, Node2, Rightnode1find, rightnode2find); + if(Rightnode! =NULL) A returnRightnode; at -Node1find = Leftnode1find | |Rightnode1find; -Node2find = Leftnode2find | |Rightnode2find; - - if(Node1find &&node2find) - returnRoot; in - if(Root = =Node1) toNode1find =true; + - if(Root = =Node2) theNode2find =true; * $ returnNULL;Panax Notoginseng}
"Turn" to find the nearest common parent node for two nodes of binary tree