You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java
+49-22
Original file line number
Diff line number
Diff line change
@@ -57,15 +57,57 @@ private static TreeNode findFCA(TreeNode root, TreeNode a, TreeNode b) {
57
57
}
58
58
}
59
59
60
-
privatestaticclassTreeNode {
60
+
publicclassTreeNode {
61
61
intval;
62
62
TreeNodeleft;
63
63
TreeNoderight;
64
-
65
-
TreeNode(intval) {
64
+
publicTreeNode(intval) {
66
65
this.val = val;
67
66
}
67
+
68
+
publicvoidsetLeft(TreeNodeleft) {
69
+
this.left = left;
70
+
}
71
+
publicTreeNodegetLeft() {
72
+
returnleft;
73
+
}
74
+
75
+
publicvoidsetRight(TreeNoderight) {
76
+
this.right = right;
77
+
}
78
+
79
+
publicTreeNodegetRight() {
80
+
returnright;
81
+
}
82
+
publicintgetVal() {
83
+
returnval;
84
+
}
68
85
}
86
+
87
+
// addNode
88
+
publicvoidaddNode(TreeNodetreeRoot) {
89
+
treeRoot.left = newTreeNode(5);
90
+
treeRoot.right = newTreeNode(8);
91
+
treeRoot.left.left = newTreeNode(1);
92
+
treeRoot.left.right = newTreeNode(3);
93
+
treeRoot.left.left.left = newTreeNode(0);
94
+
treeRoot.right.left = newTreeNode(2);
95
+
treeRoot.right.right = newTreeNode(9);
96
+
treeRoot.right.left.right = newTreeNode(7);
97
+
}
98
+
99
+
// test
100
+
publicvoidtestCase(TreeNodetreeRoot) {
101
+
System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val);
102
+
System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val);
103
+
System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val);
104
+
System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val);
105
+
System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val);
106
+
System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val);
107
+
System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val);
108
+
System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val);
109
+
System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, newTreeNode(10)).val); // this use case does not work with the above algorithm
110
+
}
69
111
70
112
publicstaticvoidmain(String[] args) {
71
113
/*
@@ -80,24 +122,9 @@ public static void main(String[] args) {
80
122
0 7
81
123
82
124
*/
83
-
TreeNodetreeRoot = newTreeNode(4);
84
-
treeRoot.left = newTreeNode(5);
85
-
treeRoot.right = newTreeNode(8);
86
-
treeRoot.left.left = newTreeNode(1);
87
-
treeRoot.left.right = newTreeNode(3);
88
-
treeRoot.left.left.left = newTreeNode(0);
89
-
treeRoot.right.left = newTreeNode(2);
90
-
treeRoot.right.right = newTreeNode(9);
91
-
treeRoot.right.left.right = newTreeNode(7);
92
-
93
-
System.out.println("FCA of 0 and 7 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.left.right).val);
94
-
System.out.println("FCA of 0 and 9 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.right.right).val);
95
-
System.out.println("FCA of 0 and 1 is: " + findFCA(treeRoot, treeRoot.left.left.left, treeRoot.left.left).val);
96
-
System.out.println("FCA of 1 and 2 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left).val);
97
-
System.out.println("FCA of 1 and 7 is: " + findFCA(treeRoot, treeRoot.left.left, treeRoot.right.left.right).val);
98
-
System.out.println("FCA of 4 and 7 is: " + findFCA(treeRoot, treeRoot, treeRoot.right.left.right).val);
99
-
System.out.println("FCA of 5 and 2 is: " + findFCA(treeRoot, treeRoot.left, treeRoot.right.left).val);
100
-
System.out.println("FCA of 7 and 9 is: " + findFCA(treeRoot, treeRoot.right.left.right, treeRoot.right.right).val);
101
-
System.out.println("FCA of 7 and 10 is: " + findFCA(treeRoot, treeRoot.right.left.right, newTreeNode(10)).val); // this use case does not work with the above algorithm
0 commit comments