7
7
8
8
import java .util .*;
9
9
10
-
11
10
/**
12
11
* This is a util class to contain all tree related methods.
13
12
*/
14
13
public class TreeUtils {
15
-
16
- /**
17
- * This method is to construct a normal binary tree. The input reads like
18
- * this for [5, 3, 6, 2, 4, null, null, 1]:
19
-
20
- 5
21
- / \
22
- 3 6
23
- / \ / \
24
- 2 4 # #
25
- /
26
- 1
27
-
28
- */
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]." )
32
- public static TreeNode constructBinaryTree (List <Integer > treeValues ) {
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 ();
38
- if (treeValues .get (i ) != null ) {
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 );
45
- }
14
+ /**
15
+ * This method is to construct a normal binary tree. The input reads like
16
+ * this for [5, 3, 6, 2, 4, null, null, 1]:
17
+ 5
18
+ / \
19
+ 3 6
20
+ / \ / \
21
+ 2 4 # #
22
+ /
23
+ 1
24
+ */
25
+ @ Notes (context = "This is usually how Leetcode OJ passes a binary tree into testing: "
26
+ + "https://leetcode.com/faq/#binary-tree, I wrote this function for my own ease of testing when copying"
27
+ + "the test case from Leetcode in the form of [1, null, 2, 3]." )
28
+ public static TreeNode constructBinaryTree (List <Integer > treeValues ) {
29
+ TreeNode root = new TreeNode (treeValues .get (0 ));
30
+ Queue <TreeNode > queue = new LinkedList <>();
31
+ queue .offer (root );
32
+ for (int i = 1 ; i < treeValues .size (); i ++) {
33
+ TreeNode curr = queue .poll ();
34
+ if (treeValues .get (i ) != null ) {
35
+ curr .left = new TreeNode (treeValues .get (i ));
36
+ queue .offer (curr .left );
37
+ }
38
+ if (++i < treeValues .size () && treeValues .get (i ) != null ) {
39
+ curr .right = new TreeNode (treeValues .get (i ));
40
+ queue .offer (curr .right );
46
41
}
47
- return root ;
48
42
}
49
-
43
+ return root ;
44
+ }
45
+
50
46
public static void printBinaryTree (TreeNode root ) {
51
47
CommonUtils .println ("\n Printing out the binary tree in a very visual manner as below:\n " );
52
-
48
+
53
49
// imitating from BTreePrinter class
54
50
int maxLevel = TreeUtils .maxLevel (root );
55
51
56
52
printNodeInternal (Collections .singletonList (root ), 1 , maxLevel );
57
53
}
58
-
54
+
59
55
private static int maxLevel (TreeNode root ) {
60
56
if (root == null ) {
61
57
return 0 ;
@@ -64,7 +60,7 @@ private static int maxLevel(TreeNode root) {
64
60
return Math .max (TreeUtils .maxLevel (root .left ),
65
61
TreeUtils .maxLevel (root .right )) + 1 ;
66
62
}
67
-
63
+
68
64
private static void printNodeInternal (
69
65
List <TreeNode > list , int level , int maxLevel ) {
70
66
if (list .isEmpty () || CommonUtils .isAllElementsNull (list ))
@@ -122,13 +118,13 @@ private static void printNodeInternal(
122
118
123
119
printNodeInternal (newNodes , level + 1 , maxLevel );
124
120
}
125
-
126
- public static void inOrderTraversal (TreeNode root ){
121
+
122
+ public static void inOrderTraversal (TreeNode root ) {
127
123
inOrder (root );
128
124
}
129
125
130
126
private static void inOrder (TreeNode root ) {
131
- if (root == null ) {
127
+ if (root == null ) {
132
128
return ;
133
129
}
134
130
inOrder (root .left );
0 commit comments