-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestCommonAncestor.java
More file actions
49 lines (38 loc) · 1.64 KB
/
Copy pathLowestCommonAncestor.java
File metadata and controls
49 lines (38 loc) · 1.64 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package LeetcodePractice;
public class LowestCommonAncestor {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) { this.val = val; }
}
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;
return left != null ? left : right;
}
public static void main(String[] args) {
LowestCommonAncestor solution = new LowestCommonAncestor();
// Construct the tree: [3,5,1,6,2,0,8,null,null,7,4]
TreeNode root = new TreeNode(3);
root.left = new TreeNode(5);
root.right = new TreeNode(1);
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(2);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(8);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);
TreeNode p = root.left; // Node with value 5
TreeNode q = root.right; // Node with value 1
TreeNode lca = solution.lowestCommonAncestor(root, p, q);
System.out.println("LCA of " + p.val + " and " + q.val + " is: " + lca.val);
// Example 2: LCA of 5 and 4
p = root.left; // 5
q = root.left.right.right; // 4
lca = solution.lowestCommonAncestor(root, p, q);
System.out.println("LCA of " + p.val + " and " + q.val + " is: " + lca.val);
}
}