最近公共祖先I
描述:
給定一棵二叉樹,找到兩個(gè)節(jié)點(diǎn)的最近公共父節(jié)點(diǎn) (LCA)。最近公共祖先是兩個(gè)節(jié)點(diǎn)的公共的祖先節(jié)點(diǎn)且具有最大深度。
! 注意事項(xiàng) : 假設(shè)給出的兩個(gè)節(jié)點(diǎn)都在樹中存在
樣例 :
對于下面這棵二叉樹
4
/ \
3 7
/ \
5 6
輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
代碼實(shí)現(xiàn):
/**
* 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
描述:
給一棵二叉樹和二叉樹中的兩個(gè)節(jié)點(diǎn),找到這兩個(gè)節(jié)點(diǎn)的最近公共祖先LCA。
兩個(gè)節(jié)點(diǎn)的最近公共祖先,是指兩個(gè)節(jié)點(diǎn)的所有父親節(jié)點(diǎn)中(包括這兩個(gè)節(jié)點(diǎn)),離這兩個(gè)節(jié)點(diǎn)最近的公共的節(jié)點(diǎn)。每個(gè)節(jié)點(diǎn)除了左右兒子指針以外,還包含一個(gè)父親指針parent,指向自己的父親。
樣例:
對于下面的這棵二叉樹
4
/ \
3 7
/ \
5 6
輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
代碼實(shí)現(xiàn):
/**
* 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
描述:
給一棵二叉樹和二叉樹中的兩個(gè)節(jié)點(diǎn),找到這兩個(gè)節(jié)點(diǎn)的最近公共祖先 LCA。
兩個(gè)節(jié)點(diǎn)的最近公共祖先,是指兩個(gè)節(jié)點(diǎn)的所有父親節(jié)點(diǎn)中(包括這兩個(gè)節(jié)點(diǎn)),離這兩個(gè)節(jié)點(diǎn)最近的公共的節(jié)點(diǎn)。返回 null 如果兩個(gè)節(jié)點(diǎn)在這棵樹上不存在最近公共祖先的話。
樣例:
給出下面這棵樹:
4
/ \
3 7
/ \
5 6
輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
LCA(5, 8) = null
代碼實(shí)現(xiàn):
/**
* 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);
}
}