-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertBinaryTree_226.java
More file actions
73 lines (64 loc) · 2.01 KB
/
Copy pathInvertBinaryTree_226.java
File metadata and controls
73 lines (64 loc) · 2.01 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
package com.leetcode.tree;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by charles on 2/27/17.
* Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
*/
public class InvertBinaryTree_226 {
/** recursion */
public TreeNode invertTree(TreeNode root) {
return invertHelper(root);
}
public TreeNode invertHelper(TreeNode node) {
if (node == null) {
return node;
}
TreeNode left = invertHelper(node.left);
TreeNode right = invertHelper(node.right);
node.left = right;
node.right = left;
return node;
}
/** Iteration solution
* The idea is that we need to swap the left and right child of all nodes in the tree
* so we create a queue to store nodes whose left and right child have not been swapped yet.
* Initially, only the root is in the queue, as long as the queue is not empty, remove the next node from the queue
* swap its children, and add the children to the queue. NULL nodes are not added to the queue.
* Eventually the queue will be empty and all the children swapped. and we return the original root
* */
public TreeNode invertTreeII(TreeNode root) {
if (root == null) {
return root;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode curr = queue.poll();
TreeNode tmp = curr.left;
curr.left = curr.right;
curr.right = tmp;
if (curr.left != null) {
queue.offer(curr.left);
}
if (curr.right != null) {
queue.offer(curr.right);
}
}
return root;
}
}