-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC226_InvertBinaryTree.java
More file actions
76 lines (59 loc) · 1.86 KB
/
LC226_InvertBinaryTree.java
File metadata and controls
76 lines (59 loc) · 1.86 KB
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
package practise.leetCode;
import java.util.*;
public class LC226_InvertBinaryTree {
// Definition for a binary tree node.
// static class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
//
// TreeNode() {}
//
// TreeNode(int val) {
// this.val = val;
// }
//
// TreeNode(int val, TreeNode left, TreeNode right) {
// this.val = val;
// this.left = left;
// this.right = right;
// }
// }
// Invert Binary Tree
public static TreeNode invertTree(TreeNode root) {
if (root == null)
return null;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.right);
invertTree(root.left);
return root;
}
// Level-order traversal to print tree as array
public static List<Integer> levelOrder(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
result.add(node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
return result;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(
4,
new TreeNode(2, new TreeNode(1), new TreeNode(3)),
new TreeNode(7, new TreeNode(6), new TreeNode(9))
);
System.out.println("Original Tree:");
System.out.println(levelOrder(root)); // [4, 2, 7, 1, 3, 6, 9]
invertTree(root);
System.out.println("Inverted Tree:");
System.out.println(levelOrder(root)); // [4, 7, 2, 9, 6, 3, 1]
}
}