/
Problem226_invertBinaryTree.java
78 lines (68 loc) · 1.76 KB
/
Problem226_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
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.longluo.top100;
import com.longluo.datastructure.TreeNode;
import java.util.LinkedList;
import java.util.Queue;
/**
* 226. 翻转二叉树
* <p>
* 翻转一棵二叉树。
* <p>
* 示例:
* 输入:
* <p>
* 4
* / \
* 2 7
* / \ / \
* 1 3 6 9
* 输出:
* <p>
* 4
* / \
* 7 2
* / \ / \
* 9 6 3 1
* 备注:
* 这个问题是受到 Max Howell 的 原问题 启发的 :
* 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
* <p>
* https://leetcode-cn.com/problems/invert-binary-tree/
* <p>
* https://leetcode.com/problems/invert-binary-tree/
*/
public class Problem226_invertBinaryTree {
// Recursion time: O(n) space: O(n)
public static TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
// BFS time: O(n) space: O(n)
public static TreeNode invertTree_bfs(TreeNode root) {
if (root == null) {
return null;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
TreeNode temp = node.right;
node.right = node.left;
node.left = temp;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return root;
}
public static void main(String[] args) {
}
}