*[?] Lowest Common Ancestor of the Nodes in a Binary Tree

Source: Internet
Author: User

Reference:

http://blog.csdn.net/v_july_v/article/details/18312089

Http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-ii.html

Http://www.cnblogs.com/springfor/p/4141924.html

(1) is the tree a BST or not?

BST, we can think in terms of the definition of BST. The currently traversed node is larger than the two points we are looking for, indicating that the two points are on the left side of the current node, so the ancestors of the two node must be on the left side of the current node, so go to the left. Conversely, look to the right. If either of these points is greater than the current node one is less than the current node, the current node is the LCA. If these two points are the ancestors of another, then this point is the LCA.

The code is as follows:

1  PublicTreeNode lowestcommonancestor (TreeNode root, TreeNode p, TreeNode q) {2         if(P.val < Root.val && Q.val <root.val)3             returnlowestcommonancestor (Root.left, p, q);4         Else if(P.val > Root.val && q.val >root.val)5             returnlowestcommonancestor (Root.right, p, q);6         Else7             returnRoot;8}

(2) What if it's not BST? Generic binary Tree.

A. There is no parent domain in the tree structure.

Find it recursively.

The code is as follows:

1 //worst case O (n)2 node Getlca (node root, node Node1, node Node2) {3         if(Root = =NULL)4             return NULL5         if(Root = = Node1 | | root = =Node2)6             returnRoot;7         8Node left =Getlca (Root.left, Node1, node2);9Node right =Getlca (Root.right, Node1, node2);Ten          One         if(Left! =NULL&& Right! =NULL) A             returnRoot; -         Else if(Left! =NULL) -             returnLeft ; the         Else if(Right! =NULL) -             returnRight ; -         Else -             return NULL; +}

B. If there is a parent domain.

The intersection problem that translates to two linkedlist.

The code is as follows:

1 intgetheight (Node p) {2         intHeight = 0;3          while(p!=NULL){4height++;5p =p.parent;6         }7         returnheight;8     }9     Ten     voidSwapintAintb) { OneA = a +b; Ab = A-b; -A = A-b; -     } the      - Node GETLCA (node p, node Q) { -         intH1 =getheight (p); -         intH2 =getheight (q); +          -         //Q is always deeper than P +         if(H1 >H2) { A Swap (H1, H2); at Swap (P, q); -         } -          -         intdiff = h2-H1; -          -          for(inti = 0; I < diff; i++) inQ =q.parent; -          to          while(p!=NULL&& q!=NULL){ +             //Common Node -             if(p = =q) the                 returnp; *p =p.parent; $Q =q.parent;Panax Notoginseng         } -          the         returnNULL;//P and Q is not in the same tree +}

*[?] Lowest Common Ancestor of the Nodes in a Binary Tree

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.