forked from mengli/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestCommonAncestorOfBinaryTree.java
More file actions
31 lines (31 loc) · 1.03 KB
/
LowestCommonAncestorOfBinaryTree.java
File metadata and controls
31 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Given a binary tree, find the lowest common ancestor of two given nodes in the tree.
*
*
* _______3______
* / \
* ___5__ ___1__
* / \ / \
* 6 _2 0 8
* / \
* 7 4
* If you are not so sure about the definition of lowest common ancestor (LCA), please refer to my previous
* post: Lowest Common Ancestor of a Binary Search Tree (BST) or the definition of LCA here. Using the tree
* above as an example, the LCA of nodes 5 and 1 is 3. Please note that LCA for nodes 5 and 4 is 5.
*
* Hint:
* Top-down or bottom-up? Consider both approaches and see which one is more efficient.
*/
public class LowestCommonAncestorOfBinaryTree {
public TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null)
return null;
if (root == p || root == q)
return root;
TreeNode left = LCA(root.left, p, q);
TreeNode right = LCA(root.right, p, q);
if (left != null && right != null)
return root;
return left != null ? left : right;
}
}