最近公共祖先I
描述:
给定一棵二叉树,找到两个节点的最近公共父节点 (LCA)。最近公共祖先是两个节点的公共的祖先节点且具有最大深度。
! 注意事项 : 假设给出的两个节点都在树中存在
样例 :
对于下面这棵二叉树
4
/ \
3 7
/ \
5 6
输出结果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
代码实现:
/**
* 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;
* }
* }
*/
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param A and B: two nodes in a Binary.
* @return: Return the least common ancestor(LCA) of the two nodes.
*/
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
if (root == null || root == A || root == B) {
return root;
}
//divide and conquer
TreeNode left = lowestCommonAncestor(root.left, A, B);
TreeNode right = lowestCommonAncestor(root.right, A, B);
if (left != null && right != null) {
return root;
}
if (left != null) {
return left;
}
if (left == null) {
return right;
}
return null;
}
}
最近公共祖先II
描述:
给一棵二叉树和二叉树中的两个节点,找到这两个节点的最近公共祖先LCA。
两个节点的最近公共祖先,是指两个节点的所有父亲节点中(包括这两个节点),离这两个节点最近的公共的节点。每个节点除了左右儿子指针以外,还包含一个父亲指针parent,指向自己的父亲。
样例:
对于下面的这棵二叉树
4
/ \
3 7
/ \
5 6
输出结果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
代码实现:
/**
* Definition of ParentTreeNode:
*
* class ParentTreeNode {
* public ParentTreeNode parent, left, right;
* }
*/
public class Solution {
/**
* @param root: The root of the tree
* @param A, B: Two node in the tree
* @return: The lowest common ancestor of A and B
*/
public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root,
ParentTreeNode A,
ParentTreeNode B) {
ArrayList<ParentTreeNode> pathA = getPath2Root(A);
ArrayList<ParentTreeNode> pathB = getPath2Root(B);
int indexA = pathA.size() - 1;
int indexB = pathB.size() - 1;
ParentTreeNode lowestAncestor = null;
while (indexA >= 0 && indexB >= 0) {
if (pathA.get(indexA) != pathB.get(indexB)) {
break;
}
lowestAncestor = pathA.get(indexA);
indexA--;
indexB--;
}
return lowestAncestor;
}
private ArrayList<ParentTreeNode> getPath2Root(ParentTreeNode node) {
ArrayList<ParentTreeNode> path = new ArrayList<>();
while (node != null) {
path.add(node);
node = node.parent;
}
return path;
}
}
最近公共祖先 III
描述:
给一棵二叉树和二叉树中的两个节点,找到这两个节点的最近公共祖先 LCA。
两个节点的最近公共祖先,是指两个节点的所有父亲节点中(包括这两个节点),离这两个节点最近的公共的节点。返回 null 如果两个节点在这棵树上不存在最近公共祖先的话。
样例:
给出下面这棵树:
4
/ \
3 7
/ \
5 6
输出结果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
LCA(5, 8) = null
代码实现:
/**
* 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
// 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);
}
}