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

Commit dae9971

Browse files
fix constructBinaryTree utils
1 parent dc901e3 commit dae9971

File tree

1 file changed

+28
-53
lines changed

1 file changed

+28
-53
lines changed

src/main/java/com/fishercoder/common/utils/TreeUtils.java

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* This is a util class to contain all tree related methods.
1313
*/
1414
public class TreeUtils {
15-
15+
1616
/**
1717
* This method is to construct a normal binary tree. The input reads like
1818
* this for [5, 3, 6, 2, 4, null, null, 1]:
@@ -21,64 +21,32 @@ public class TreeUtils {
2121
/ \
2222
3 6
2323
/ \ / \
24-
2 4 N N
24+
2 4 # #
2525
/
2626
1
2727
28-
* where N is null.
29-
*
30-
* Basically you go from top level to bottom, then left to right within the level
31-
*
32-
* Cool! Confirmed/tested out that this one does exactly the same as the bruteforce one does!
3328
*/
34-
@Notes(issue = "This is usually how Leetcode OJ passes a binary tree into testing.")
29+
@Notes(context = "This is usually how Leetcode OJ passes a binary tree into testing: " +
30+
"https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying" +
31+
"the test case from Leetcode in the form of [1, null, 2, 3].")
3532
public static TreeNode constructBinaryTree(List<Integer> treeValues) {
36-
TreeNode root = new TreeNode(null, treeValues.get(0), null);
37-
38-
final Queue<TreeNode> queue = new LinkedList<TreeNode>();
39-
queue.add(root);
40-
41-
final int half = treeValues.size() / 2;
42-
43-
for (int i = 0; i < half; i++) {
33+
TreeNode root = new TreeNode(treeValues.get(0));
34+
Queue<TreeNode> queue = new LinkedList<>();
35+
queue.offer(root);
36+
for (int i = 1; i < treeValues.size(); i++) {
37+
TreeNode curr = queue.poll();
4438
if (treeValues.get(i) != null) {
45-
final TreeNode current = queue.poll();
46-
final int left = 2 * i + 1;
47-
final int right = 2 * i + 2;
48-
49-
if (treeValues.get(left) != null) {
50-
current.left = new TreeNode(null, treeValues.get(left),
51-
null);
52-
queue.add(current.left);
53-
}
54-
if (right < treeValues.size() && treeValues.get(right) != null) {
55-
current.right = new TreeNode(null, treeValues.get(right),
56-
null);
57-
queue.add(current.right);
58-
}
39+
curr.left = new TreeNode(treeValues.get(i));
40+
queue.offer(curr.left);
41+
}
42+
if (++i < treeValues.size() && treeValues.get(i) != null) {
43+
curr.right = new TreeNode(treeValues.get(i));
44+
queue.offer(curr.right);
5945
}
6046
}
6147
return root;
6248
}
6349

64-
@Notes(issue = "This brute force takes in only first seven values to construct a tree, I really need to write one method that takes any arbitrary number of values.")
65-
public static TreeNode bruteForceConstructBinaryTree(
66-
List<Integer> treeValues) {
67-
TreeNode root = null;
68-
69-
if (treeValues.size() < 7)
70-
return root;
71-
72-
root = new TreeNode(treeValues.get(0));
73-
root.left = new TreeNode(treeValues.get(1));
74-
root.right = new TreeNode(treeValues.get(2));
75-
root.left.left = new TreeNode(treeValues.get(3));
76-
root.left.right = new TreeNode(treeValues.get(4));
77-
root.right.left = new TreeNode(treeValues.get(5));
78-
root.right.right = new TreeNode(treeValues.get(6));
79-
return root;
80-
}
81-
8250
public static void printBinaryTree(TreeNode root) {
8351
CommonUtils.println("\n\nPrinting out the binary tree in a very visual manner as below:");
8452

@@ -89,8 +57,9 @@ public static void printBinaryTree(TreeNode root) {
8957
}
9058

9159
private static int maxLevel(TreeNode root) {
92-
if (root == null)
60+
if (root == null) {
9361
return 0;
62+
}
9463

9564
return Math.max(TreeUtils.maxLevel(root.left),
9665
TreeUtils.maxLevel(root.right)) + 1;
@@ -166,21 +135,27 @@ private static void inOrder(TreeNode root) {
166135
System.out.print(root.val + " ");
167136
inOrder(root.right);
168137
}
169-
138+
170139
public static void main (String... args){
171140
//test random int generator
172141
List<Integer> treeValues = CommonUtils.randomIntArrayGenerator(24);
173142

174143
List<Integer> treeValues2 = Arrays.asList(0, 1, 2, 3, 4, 5, 6);
175144

176145
//test tree construction
177-
TreeNode root1 = bruteForceConstructBinaryTree(treeValues2);
178-
inOrderTraversal(root1);
179-
printBinaryTree(root1);
146+
// TreeNode root1 = bruteForceConstructBinaryTree(treeValues2);
147+
// inOrderTraversal(root1);
148+
// printBinaryTree(root1);
180149

181150
// test tree construction
182151
TreeNode root2 = constructBinaryTree(treeValues);
183152
inOrderTraversal(root2);
184153
printBinaryTree(root2);
154+
155+
List<Integer> treeVals = new ArrayList<>(Arrays.asList(1, null, 2, 3));
156+
CommonUtils.printList(treeVals);
157+
root2 = constructBinaryTree(treeVals);
158+
// inOrderTraversal(root2);
159+
printBinaryTree(root2);
185160
}
186161
}

0 commit comments

Comments
 (0)