Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2005a9b

Browse files
committed
edit FirstCommonAncestor.java
1 parent 87c869b commit 2005a9b

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

src/main/java/com/ctci/treesandgraphs/FirstCommonAncestor.java

+49-22
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,57 @@ private static TreeNode findFCA(TreeNode root, TreeNode a, TreeNode b) {
5757
}
5858
}
5959

60-
private static class TreeNode {
60+
public class TreeNode {
6161
int val;
6262
TreeNode left;
6363
TreeNode right;
64-
65-
TreeNode(int val) {
64+
public TreeNode(int val) {
6665
this.val = val;
6766
}
67+
68+
public void setLeft(TreeNode left) {
69+
this.left = left;
70+
}
71+
public TreeNode getLeft() {
72+
return left;
73+
}
74+
75+
public void setRight(TreeNode right) {
76+
this.right = right;
77+
}
78+
79+
public TreeNode getRight() {
80+
return right;
81+
}
82+
public int getVal() {
83+
return val;
84+
}
6885
}
86+
87+
// addNode
88+
public void addNode(TreeNode treeRoot) {
89+
treeRoot.left = new TreeNode(5);
90+
treeRoot.right = new TreeNode(8);
91+
treeRoot.left.left = new TreeNode(1);
92+
treeRoot.left.right = new TreeNode(3);
93+
treeRoot.left.left.left = new TreeNode(0);
94+
treeRoot.right.left = new TreeNode(2);
95+
treeRoot.right.right = new TreeNode(9);
96+
treeRoot.right.left.right = new TreeNode(7);
97+
}
98+
99+
// test
100+
public void testCase(TreeNode treeRoot) {
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, new TreeNode(10)).val); // this use case does not work with the above algorithm
110+
}
69111

70112
public static void main(String[] args) {
71113
/*
@@ -80,24 +122,9 @@ public static void main(String[] args) {
80122
0 7
81123
82124
*/
83-
TreeNode treeRoot = new TreeNode(4);
84-
treeRoot.left = new TreeNode(5);
85-
treeRoot.right = new TreeNode(8);
86-
treeRoot.left.left = new TreeNode(1);
87-
treeRoot.left.right = new TreeNode(3);
88-
treeRoot.left.left.left = new TreeNode(0);
89-
treeRoot.right.left = new TreeNode(2);
90-
treeRoot.right.right = new TreeNode(9);
91-
treeRoot.right.left.right = new TreeNode(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, new TreeNode(10)).val); // this use case does not work with the above algorithm
125+
FirstCommonAncestor fcancestor = new FirstCommonAncestor();
126+
FirstCommonAncestor.TreeNode treeRoot = fcancestor.new TreeNode(4);
127+
fcancestor.addNode(treeRoot);
128+
fcancestor.testCase(treeRoot);
102129
}
103130
}

0 commit comments

Comments
 (0)