[Offoffoff] Q50: the closest ancestor of the node in the tree

Source: Internet
Author: User

 
# @ Root: the root of searched tree # @ nodetofind: the tree-node to be found # @ path: the path from root to node ####@ search tree referenced by root, and return the path #from root to node, if node not exist, path = [] # @ def getpath (root, nodetofind, PATH): If (None = root or none = nodetofind): Return false # Case 1: current root = node, so insert to patlif root = nodetofind: path. insert (0, root) return true # search in left Barch and right branchbfindinleft = falsebfindinright = falseif root. left: bfindinleft = getpath (root. left, nodetofind, PATH) if false = bfindinleft and root. right: bfindinright = getpath (root. right, nodetofind, PATH) # Case 2: nodetofind in subtree of root, insert root if bfindinleft or bfindinright: path. insert (0, root) return truereturn false

The function is used to search for the nodetofind node in the tree indicated by the root user. If the node is found, the path node is added to the path when returned. There are three types of tree traversal, here we use post-order traversal to process the root account after all the conditions are known, because the root account of the current node should not be added to the Path, it is not only related to the current node root, but also to its sub-nodes. That is, if the current node is the node to be searched, it is no problem to add the current node, however, even if the current node is not the node to be searched, and its Child tree has a lookup node, the current node also needs to be added to the path. In this way, you do not need to insert the node every time. If the condition is not met, You need to pop the node.


 
Def getclosetparent (root, node1, node2): path1 = []; path2 = [] If none = root or none = node1 or none = node2: return none # Get the path from root to node1 and node2getpath (root, node1, path1) getpath (root, node2, path2) # Find closet parent of node1 and node2shorpathlen = min (LEN (path1), Len (path2) for I in range (1, shorpathlen): If path1 [I]! = Path2 [I] And path1 [I-1] = path2 [I-1]: Return path1 [I-1] Return none
In the getpath function, the obtained path starts with "root", that is, "root" is the first node in the path list. Then, we start with "root" and compare it once, find the last equal, that is, the closest common ancestor of the two.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.