-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
143 lines (116 loc) · 3.43 KB
/
BinaryTree.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package DataStructures;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* 1. build tree
2. print tree in the form of array
3. update
4. swap left and right nodes
5. invert tree
*
*
*
*
* @author Srinvas Vadige, [email protected]
* @since 23 Sept 2024
*/
public class BinaryTree {
public static void main(String[] args) {
int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TreeNode root = buildTree(nums);
/*
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ /
8 9 10
*/
printTree(root).forEach(System.out::println);
invertTree(root);
System.out.println();
printTree(root).forEach(System.out::println);
/*
1
/ \
3 2
/ \ / \
7 6 5 4
/\ /\
null 10 9 8
1 3 1 5 4 7 6 9 8 null 10
*/
}
public static TreeNode buildTree(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
TreeNode root = new TreeNode(nums[0]);
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int i = 1;
while (i < nums.length) {
TreeNode curr = q.remove();
if (i < nums.length) {
curr.left = new TreeNode(nums[i++]);
q.add(curr.left);
}
if (i < nums.length) {
curr.right = new TreeNode(nums[i++]);
q.add(curr.right);
}
}
return root;
}
public static List<List<String>> printTree(final TreeNode root) {
final int width = (int) Math.pow(2, getHeight(root)) - 1;
final List<List<String>> result = new ArrayList<>();
dfs(root, result, 0, width, 0, width);
return result;
}
private static void dfs(final TreeNode root, final List<List<String>> result, final int l, final int r, final int level, final int width) {
if(root != null) {
if(level >= result.size()) {
result.add(new ArrayList<>());
for(int i = 0; i < width; ++i)
result.get(level).add("");
}
final int mid = (l + r) / 2;
result.get(level).set(mid, String.valueOf(root.val));
dfs(root.left, result, l, mid, level + 1, width);
dfs(root.right, result, mid, r, level + 1, width);
}
}
private static int getHeight(final TreeNode root) {
if(root == null)
return 0;
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
public static TreeNode invertTree(TreeNode root) {
if (root == null) return null;
swapNodes(root);
return root;
}
public static void swapNodes(TreeNode node){
if(node == null) return;
TreeNode temp=node.left;
node.left=node.right;
node.right=temp;
swapNodes(node.left);
swapNodes(node.right);
}
}
class TreeNode {
int val;
TreeNode left, right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}