-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay14.java
More file actions
53 lines (43 loc) · 1.55 KB
/
Day14.java
File metadata and controls
53 lines (43 loc) · 1.55 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
class TreeNode {
int val;
TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
}
public class Day14 {
private TreeNode findLCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null || root == p || root == q) {
return root;
}
TreeNode left = findLCA(root.left, p, q);
TreeNode right = findLCA(root.right, p, q);
if (left != null && right != null) {
return root; // This node is the LCA
}
return (left != null) ? left : right;
}
public static void main(String[] args) {
// Create a sample binary tree
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);
Day14 lcaFinder = new Day14();
// Example usage: Find LCA of nodes 5 and 1
TreeNode p = root.left;
TreeNode q = root.right;
TreeNode lca = lcaFinder.findLCA(root, p, q);
if (lca != null) {
System.out.println("Lowest Common Ancestor of " + p.val + " and " + q.val + " is: " + lca.val);
} else {
System.out.println("One or both nodes not found in the tree.");
}
}
}