578. Lowest Common Ancestor III [LintCode]
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.
The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.
Notice
node A or node B may not exist in tree.
Example
For the following binary tree:
4 / \ 3 7 / \ 5 6
LCA(3, 5) =
4
LCA(5, 6) =
7
LCA(6, 7) =
7
solution2: 对于树的任意一个node,如果本身是p或者q的话,LCA只可能在该点的上面而不可能在下面。所以可以立刻返回该点而不用继续往下探寻。其次不存在p或者q的子树返回的一定是null。
// solution 1
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
class ResultType {
public boolean a_exist, b_exist;
public TreeNode node;
ResultType(boolean a, boolean b, TreeNode n) {
a_exist = a;
b_exist = b;
node = n;
}
}
public class Solution {
/**
* @param root The root of the binary tree.
* @param A and B two nodes
* @return: Return the LCA of the two nodes.
*/
public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode A, TreeNode B) {
// write your code here
ResultType rt = helper(root, A, B);
if (rt.a_exist && rt.b_exist)
return rt.node;
else
return null;
}
public ResultType helper(TreeNode root, TreeNode A, TreeNode B) {
if (root == null)
return new ResultType(false, false, null);
ResultType left_rt = helper(root.left, A, B);
ResultType right_rt = helper(root.right, A, B);
boolean a_exist = left_rt.a_exist || right_rt.a_exist || root == A;
boolean b_exist = left_rt.b_exist || right_rt.b_exist || root == B;
if (root == A || root == B)
return new ResultType(a_exist, b_exist, root);
if (left_rt.node != null && right_rt.node != null)
return new ResultType(a_exist, b_exist, root);
if (left_rt.node != null)
return new ResultType(a_exist, b_exist, left_rt.node);
if (right_rt.node != null)
return new ResultType(a_exist, b_exist, right_rt.node);
return new ResultType(a_exist, b_exist, null);
}
}
// solution 2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
}
if (left != null) {
return left;
}
return right;
}
}
相似的题:235. Lowest Common Ancestor of a Binary Search Tree 是在BST中搜索
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
}
if (p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
}