-
Notifications
You must be signed in to change notification settings - Fork 1
/
InvertBinaryTree.java
65 lines (56 loc) · 1.75 KB
/
InvertBinaryTree.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
import java.util.Deque;
import java.util.LinkedList;
public class InvertBinaryTree {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
preorder(root);
///////// TEST DIFFERENT METHODS
root = invertIteratively(root);
//////// TEST DIFFERENT METHODS
System.out.println();
preorder(root);
}
//BFS
public static void preorder(TreeNode root) {
if(root == null) return;
System.out.print(root.val + " ");
preorder(root.left);
preorder(root.right);
}
public static TreeNode invertTreedfs(TreeNode root) {
if(root == null) return root;
TreeNode left = root.left, right = root.right;
root.right = invertTreedfs(left);
root.left = invertTreedfs(right);
return root;
}
public static TreeNode invertIteratively(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode head = stack.pop();
TreeNode temp = head.left;
head.left = head.right;
head.right = temp;
if(head.left!=null) {
stack.push(head.left);
} else if(head.right!=null){
stack.push(head.right);
}
}
return root;
}
static class TreeNode {
TreeNode left;
TreeNode right;
int val;
TreeNode() {};
TreeNode(int val) {this.val = val;}
}
}