Repository navigation
Expand file tree
/
Copy pathproblem236.java
More file actions
94 lines (65 loc) · 2.02 KB
/
Copy pathproblem236.java
File metadata and controls
94 lines (65 loc) · 2.02 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//236. Lowest Common Ancestor of a Binary Tree
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class problem236 {
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null)
return null;
if(root== p || root ==q)
{
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right= lowestCommonAncestor(root.right, p, q);
if(left==null)
return right;
if(right==null)
return left;
return root;
}
public static TreeNode buildTree(Integer[] arr) {
if (arr.length == 0 || arr[0] == null) return null;
TreeNode root = new TreeNode(arr[0]);
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int i = 1;
while (!queue.isEmpty() && i < arr.length) {
TreeNode current = queue.poll();
// Left child
if (i < arr.length && arr[i] != null) {
current.left = new TreeNode(arr[i]);
queue.add(current.left);
}
i++;
// Right child
if (i < arr.length && arr[i] != null) {
current.right = new TreeNode(arr[i]);
queue.add(current.right);
}
i++;
}
return root;
}
public static void main(String[] args) {
Integer[] treeArray = {3,5,1,6,2,0,8,null,null,7,4};
// Construct the tree
TreeNode root = buildTree(treeArray);
TreeNode p = root.left;
TreeNode q = root.right;
System.out.println(lowestCommonAncestor(root,p , q).val);
}
}