12
12
* This is a util class to contain all tree related methods.
13
13
*/
14
14
public class TreeUtils {
15
-
15
+
16
16
/**
17
17
* This method is to construct a normal binary tree. The input reads like
18
18
* this for [5, 3, 6, 2, 4, null, null, 1]:
@@ -21,64 +21,32 @@ public class TreeUtils {
21
21
/ \
22
22
3 6
23
23
/ \ / \
24
- 2 4 N N
24
+ 2 4 # #
25
25
/
26
26
1
27
27
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!
33
28
*/
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]." )
35
32
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 ();
44
38
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 );
59
45
}
60
46
}
61
47
return root ;
62
48
}
63
49
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
-
82
50
public static void printBinaryTree (TreeNode root ) {
83
51
CommonUtils .println ("\n \n Printing out the binary tree in a very visual manner as below:" );
84
52
@@ -89,8 +57,9 @@ public static void printBinaryTree(TreeNode root) {
89
57
}
90
58
91
59
private static int maxLevel (TreeNode root ) {
92
- if (root == null )
60
+ if (root == null ) {
93
61
return 0 ;
62
+ }
94
63
95
64
return Math .max (TreeUtils .maxLevel (root .left ),
96
65
TreeUtils .maxLevel (root .right )) + 1 ;
@@ -166,21 +135,27 @@ private static void inOrder(TreeNode root) {
166
135
System .out .print (root .val + " " );
167
136
inOrder (root .right );
168
137
}
169
-
138
+
170
139
public static void main (String ... args ){
171
140
//test random int generator
172
141
List <Integer > treeValues = CommonUtils .randomIntArrayGenerator (24 );
173
142
174
143
List <Integer > treeValues2 = Arrays .asList (0 , 1 , 2 , 3 , 4 , 5 , 6 );
175
144
176
145
//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);
180
149
181
150
// test tree construction
182
151
TreeNode root2 = constructBinaryTree (treeValues );
183
152
inOrderTraversal (root2 );
184
153
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 );
185
160
}
186
161
}
0 commit comments