-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvertBinaryTree.java
65 lines (55 loc) · 1.3 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
package algorithm.topics.binarytree;
import algorithm.leetcode.domain.TreeNode;
/**
* Invert a binary tree.
* <pre>
* 4
* / \
* 2 7
* / \ / \
* 1 3 6 9
*
* </pre>
* to
* <pre>
* 4
* / \
* 7 2
* / \ / \
* 9 6 3 1
* </pre>
* <p>
* <p>
* <a href="https://leetcode.cn/problems/invert-binary-tree/">Invert Binary Tree</a>
* Difficulty: Easy
* <p>
* Trivia:
* This problem was inspired by this original tweet by Max Howell:
* <pre>
* 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.
* </pre>
*
* @author hufeng
* @version InvertBinaryTree.java, v 0.1 28/11/2017 10:17 PM Exp $
*/
public class InvertBinaryTree {
public static TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
invertBinaryTree(root);
return root;
}
private static void invertBinaryTree(TreeNode node) {
if (node == null) {
return;
}
// swap left node and right node
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
invertBinaryTree(node.left);
invertBinaryTree(node.right);
}
}