Lowest Common Ancestor of Two Nodes in a Binary Tree
2014-12-04 08:16
323 查看
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。
代码如下:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val < root.val && q.val < root.val)
return lowestCommonAncestor(root.left, p, q);
else if(p.val > root.val && q.val > root.val)
return lowestCommonAncestor(root.right, p, q);
else
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);
if(left != null && right != null)
return root;
else if(left != null)
return left;
else if(right != null)
return right;
else
return null;
}
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
void swap(int a, int b){
a = a + b;
b = a - b;
a = a - b;
}
Node getLCA(Node p, Node q){
int h1 = getHeight(p);
int h2 = getHeight(q);
//q is always deeper than p
if(h1 > h2){
swap(h1, h2);
swap(p, q);
}
int diff = h2 - h1;
for( int i = 0; i < diff; i++)
q = q.parent;
while(p!=null && q!=null){
//common node
if(p == q)
return p;
p = p.parent;
q = q.parent;
}
return NULL; //p and q are not in the same tree
}
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。
代码如下:
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(p.val < root.val && q.val < root.val)
return lowestCommonAncestor(root.left, p, q);
else if(p.val > root.val && q.val > root.val)
return lowestCommonAncestor(root.right, p, q);
else
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);
if(left != null && right != null)
return root;
else if(left != null)
return left;
else if(right != null)
return right;
else
return null;
}
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
void swap(int a, int b){
a = a + b;
b = a - b;
a = a - b;
}
Node getLCA(Node p, Node q){
int h1 = getHeight(p);
int h2 = getHeight(q);
//q is always deeper than p
if(h1 > h2){
swap(h1, h2);
swap(p, q);
}
int diff = h2 - h1;
for( int i = 0; i < diff; i++)
q = q.parent;
while(p!=null && q!=null){
//common node
if(p == q)
return p;
p = p.parent;
q = q.parent;
}
return NULL; //p and q are not in the same tree
}
相关文章推荐
- **Lowest Common Ancestor of Two Nodes in a Binary Tree
- Lowest Common Ancestor of The Two Nodes in a Binary Tree
- Find the common ancestor of two nodes in a binary tree
- Find the nearest common ancestor of any two nodes in a binary search tree
- Lowest Common Ancestor (LCA) of two nodes in graph
- Summary: Lowest Common Ancestor in a Binary Tree & Shortest Path In a Binary Tree
- Lowest Common Ancestor in Binary Tree
- Lowest Common Ancestor of a Binary Tree Part I
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Tree, with Parent Pointer
- My Solution to Lowest Common Ancestor of a Binary Tree Part I(Top-Down Approach)
- leetcode - Lowest Common Ancestor of a Binary Tree
- My Solution to Lowest Common Ancestor of a Binary Tree Part I(Bottom-up Approach)
- Leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Tree Part I
- 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]
- leetcode A1: Lowest Common Ancestor of a Binary Tree Part I
- leetcode A1: Lowest Common Ancestor of a Binary Tree Part I
- Lowest Common Ancestor of Binary Search Tree (BST)
- 二叉搜索树(BST)的最近公共祖先(LCA)问题(Lowest Common Ancestor of a Binary Tree)