Lowest Common Ancestor of Two Nodes in a Binary Tree


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

(1) Is the tree a BST or not?

     BST的話,我們就能按照BST的定義思考了。當前遍歷的node如果比我們要找的兩個點都大,說明那兩個點都在當前node的左邊,所以這兩個node的祖先肯定在當前node的左邊,所以往左邊找。反之,往右邊找。如果這兩個點一個大於當前node一個小於當前node,說明當前node就是LCA。 如果這兩個點一個是另一個的祖先,那么這個點就是LCA。

代碼如下:

 

1      public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
2          if(p.val < root.val && q.val < root.val)
3              return lowestCommonAncestor(root.left, p, q);
4          else  if(p.val > root.val && q.val > root.val)
5              return lowestCommonAncestor(root.right, p, q);
6          else
7              return root;

8     } 

 

(2)那如果不是BST呢?一般Binary Tree.

   a. Tree結構中沒有parent域。

       遞歸找。

       代碼如下:

      

 1  // Worst case O(n)
 2  Node getLCA(Node root, Node node1, Node node2){
 3          if(root ==  null)
 4              return  null
 5          if(root == node1 || root == node2)
 6              return root;
 7         
 8         Node left = getLCA(root.left, node1, node2);
 9         Node right = getLCA(root.right, node1, node2);
10         
11          if(left !=  null && right !=  null)
12              return root;
13          else  if(left !=  null)
14              return left;
15          else  if(right !=  null)
16              return right;
17          else
18              return  null;
19     }

 

 b. 如果有parent域。

    轉化為兩個linkedlist的交點問題。

    代碼如下:

   

 1  int getHeight(Node p){
 2          int height = 0;
 3          while(p!= null){
 4             height++;
 5             p = p.parent;
 6         }
 7          return height;
 8     }
 9     
10      void swap( int a,  int b){
11         a = a + b;
12         b = a - b;
13         a = a - b;
14     }
15     
16     Node getLCA(Node p, Node q){
17          int h1 = getHeight(p);
18          int h2 = getHeight(q);
19         
20          // q is always deeper than p
21           if(h1 > h2){
22             swap(h1, h2);
23             swap(p, q);
24         }
25         
26          int diff = h2 - h1;
27         
28          forint i = 0; i < diff; i++)
29             q = q.parent;
30         
31          while(p!= null && q!= null){
32              // common node
33               if(p == q)
34                  return p;
35             p = p.parent;
36             q = q.parent;
37         }
38         
39          return NULL;  // p and q are not in the same tree
40      }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM