-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathFirstCommonAncestor.java
130 lines (116 loc) · 4.67 KB
/
FirstCommonAncestor.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.ctci.treesandgraphs;
/**
* Design an algorithm and write code to find the first common ancestor of two nodes in a binary
* tree. Avoid storing additional nodes in a data structure. Also, for this question, the tree node
* does NOT have access to its parent node. NOTE: This is not necessarily a binary search tree.
*
* First Common Ancestor or the Least/Lowest Common Ancestor of two nodes is a node which is the
* closest to both of the nodes.
*
* @author rampatra
* @since 2019-02-24
*/
public class FirstCommonAncestor {
/**
* We recurse through the entire tree with a function called findFCA(TreeNode root, TreeNode TreeNode a, TreeNode b).
* This function returns values as follows:
* - Returns p, if root's subtree includes p (and not q).
* - Returns q, if root's subtree includes q (and not p).
* - Returns null, if neither p nor q are in root's subtree.
* - Else, returns the common ancestor of p and q.
* <p>
* See {@link com.rampatra.trees.LeastCommonAncestorInBT} for a better answer.
*
* @param root
* @param a
* @param b
* @return the least common ancestor node
*/
private static TreeNode findFCA(TreeNode root, TreeNode a, TreeNode b) {
if (root == null) { // validation
return null;
}
if (root == a && root == b) { // optimization
return root;
}
TreeNode left = findFCA(root.left, a, b);
if (left != null && left != a && left != b) {
return left;
}
TreeNode right = findFCA(root.right, a, b);
if (right != null && right != a && right != b) {
return right;
}
/* One node is found on the left subtree and other on the
right one. This means current node is the ancestor. */
if (left != null && right != null) {
return root;
} else if (root == a || root == b) {
return root;
} else {
return left == null ? right : left;
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getLeft() {
return left;
}
public void setRight(TreeNode right) {
this.right = right;
}
public TreeNode getRight() {
return right;
}
public int getVal() {
return val;
}
}
// addNode
public void addNode(TreeNode treeRoot) {
treeRoot.left = new TreeNode(5);
treeRoot.right = new TreeNode(8);
treeRoot.left.left = new TreeNode(1);
treeRoot.left.right = new TreeNode(3);
treeRoot.left.left.left = new TreeNode(0);
treeRoot.right.left = new TreeNode(2);
treeRoot.right.right = new TreeNode(9);
treeRoot.right.left.right = new TreeNode(7);
}
// test
public void testCase(TreeNode treeRoot) {
System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val);
System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val);
System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val);
System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val);
System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val);
System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val);
System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val);
System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val);
System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, new TreeNode(10)).val); // this use case does not work with the above algorithm
}
public static void main(String[] args) {
/*
The binary tree looks like:
4
/ \
5 8
/ \ / \
1 3 2 9
/ \
0 7
*/
FirstCommonAncestor fcancestor = new FirstCommonAncestor();
FirstCommonAncestor.TreeNode treeRoot = fcancestor.new TreeNode(4);
fcancestor.addNode(treeRoot);
fcancestor.testCase(treeRoot);
}
}