A very popular question, common in various interviews, http://fayaa.com/tiku/view/16/
Here is a good summary.
Find the closest public parent node of the two nodes in the binary tree.
|
Case 1: The node only has left/right, and there is no parent pointer. The root is known. Case 2: Unknown root, but each node has a parent pointer Case 3. A binary tree is a binary search tree, and the value of root and two nodes (A, B) is known. |
Although case 1 is the first case, it seems complicated. Let's start with the second case.
10
//
6 14
////
4 8 12 16
//
3 5
Draw a binary tree as an example. If we want to find the common Father's Day points on nodes 3 and 8, we will first find the path strength from 3 to the root node, and then find the path from 8 to the root node.
10
//
/
6 14
/
/
//
4 8 12 16
/
/
3 5
The 3 path is marked in red, and the 8 path is marked in Green. We can see that the problem here is actually another well-known problem. There are two single-chain tables that intersect each other to find their intersection points!
You just need to look at the image of this binary tree, or you can see it by looking at the neck. :) that method is also a traditional method for finding the length of listing a, lengtha, and lengthb. Then let the long linked list go through the step of ABS (lengtha-lengthb) and go hand in hand to solve the problem.
Int getlength (bstnode * pnode) <br/>{< br/> int length = 0; <br/> bstnode * ptemp = pnode; <br/> while (ptemp) <br/>{< br/> length ++; <br/> ptemp = ptemp-> pparent; <br/>}< br/> return length; <br/>}< br/> bstnode * findlcacase2 (bstnode * pnode1, bstnode * pnode2) <br/>{< br/> int length1 = getlength (pnode1 ); <br/> int lengh2 = getlength (pnode2); </P> <p> // skip the ABS (length1-length2) <br/> bstnode * piter1 = N Ull; <br/> bstnode * piter2 = NULL; <br/> int K = 0; <br/> If (length1> = leng22) <br/> {<br/> bstnode * ptemp = pnode1; <br/> while (K ++ <length1-length2) <br/>{< br/> ptemp = ptemp-> pparent; <br/>}< br/> piter1 = ptemp; <br/> piter2 = pnode2; <br/>}< br/> else <br/>{< br/> bstnode * ptemp = pnode1; <br/> while (K ++ <length2-length1) <br/>{< br/> ptemp = ptemp-> pparent; <br/>}< br/> piter1 = pnode1; <br/> piter2 = Pte MP; <br/>}</P> <p> while (piter1 & piter2 & piter1! = Piter2) <br/>{< br/> piter1 = piter1-> pparent; <br/> piter2 = piter2-> pparent; <br/>}< br/> return piter1; <br/>}
I wrote a code myself, and I always felt a little redundant. I hope some people can help me rewrite the code more harmoniously after reading the article.
In the original figure, Case 3: If it is a binary search tree and the root and A, B are known, we assume that a, B is 3, 8. From knowing the root condition, we naturally think of recursion (not recursion, of course. The key is the convergence condition. Under what circumstances can we determine that the node checked is the latest Father's Day node? In fact, we can see some clues from this example. If the currently accessed nodes are smaller than those of A and B, it will definitely not work. If it is larger than a and B, neither can it. That is to say, this node is valid only in the range of a <= node <= B (we assume a <B ). Similar code is widely circulated on the Internet:
Bstnode * findlcacase3 (bstnode * pnode, int value1, int value2) <br/>{< br/> bstnode * ptemp = pnode; <br/> while (ptemp) <br/>{< br/> If (ptemp-> DATA> value1 & ptemp-> DATA> value2) <br/> ptemp = ptemp-> pleft; <br/> else if (ptemp-> data <value1 & ptemp-> data <value2) <br/> ptemp = ptemp-> pright; <br/> else <br/> return ptemp; <br/>}< br/> return NULL; <br/>}
Well, the previous problems have been solved. Let's look back at the first case. Only the root and left and right nodes, and no parent, are not the sorting tree. What should we do? There are also many so-called LCA and rmq algorithms circulating on the Internet. We are overwhelmed to find the most appropriate one, especially during the interview, in a specific time space, it is difficult for you to write a complicated logic (for example, you will implement a suffix tree during the interview or use dynamic planning to find the longest public substring, even if the efficiency is different, I also choose dynamic planning :)). So here, when encountering similar problems, I chose simple records to find the paths of node1 and node2, and then analyzed their paths with similar situations, for example, node1 = 3 and node2 = 8. we can certainly find the 3 node from the root node, and record the path 3, 4, 6, 10. Similarly, we can also find 8, 6, 10. We store this information in two vectors, and discard the redundant Node 3 starting from the long vector, starting from the same Residual Length, 4! = 8, 6 = 6, coooool, we found our answer. The following code is written in full accordance with this idea.
# Include <vector> <br/> bool nodepath (bstnode * proot, int value, STD: vector <bstnode *> & Path) <br/>{< br/> If (proot = NULL) return false; <br/> If (proot-> data! = Value) <br/>{< br/> If (nodepath (proot-> pleft, value, PATH) <br/>{< br/> path. push_back (proot); <br/> return true; <br/>}< br/> else <br/>{< br/> If (nodepath (proot-> pright, value, PATH) <br/>{< br/> path. push_back (proot); <br/> return true; <br/>}< br/> else <br/> return false; <br/>}< br/> else <br/> {<br/> path. push_back (proot); <br/> return true; <br/>}< br/> bstnode * findlcacase1 (bstnode * pnod E, int value1, int value2) <br/>{< br/> STD: vector <bstnode *> path1; <br/> STD :: vector <bstnode *> path2; <br/> bool find = false; <br/> Find | = nodepath (pnode, value1, path1 ); <br/> Find & = nodepath (pnode, value2, path2); <br/> bstnode * preturn = NULL; <br/> If (FIND) <br/>{< br/> int minsize = path1.size ()> path2.size ()? Path2.size (): path1.size (); <br/> int it1 = path1.size ()-minsize; <br/> int it2 = path2.size ()-minsize; <br/> for (; it1 <path1.size (), it2 <path2.size (); it1 ++, it2 ++) <br/>{< br/> If (path1 [it1] = path2 [it2]) <br/>{< br/> preturn = path1 [it1]; <br/> break; <br/>}< br/> return preturn; <br/>}< br/>
It took about 30 minutes to modify and debug the code, and then it gradually stabilized. It is really hard to imagine how it would work in the interview environment, it may be only days to know.