-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeUtil.java
More file actions
38 lines (30 loc) · 1007 Bytes
/
TreeUtil.java
File metadata and controls
38 lines (30 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
Gonna make the Binary tree generated using something like leet code tree generator
using array, I want to keep it generic, the user of the TreeNode should specify the type
*/
public class TreeUtil{
public static void main(String[] args) {
System.out.println("Tree util");
String [] arr = new String[] {"1", "0", "6" , "3"};
BinTreeNode root = generateTree(arr, 0);
printTree(root, 0);
}
public static BinTreeNode generateTree(String[] arr, int pIndex){
if (pIndex >= arr.length)
return null;
BinTreeNode root = new BinTreeNode(arr[pIndex]);
root.left = generateTree(arr, (pIndex*2) + 1 );
root.right = generateTree(arr, (pIndex*2) +2);
return root;
}
private static void printTree(BinTreeNode root, int depth) {
if (root == null) {
return;
}
for (int i = 0; i < depth; ++i)
System.out.print("\t");
System.out.print(root.val + "\n");
printTree(root.left, depth + 1);
printTree(root.right, depth + 1);
}
}