-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNO226. Invert Binary Tree.java
59 lines (58 loc) · 1.44 KB
/
NO226. Invert Binary Tree.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
/**
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.
题意:反转一个二叉树,经典题。
关于这个题有一个故事,就是著名mac系统软件包管理工具作者:
Max Howell去Google面试时,因为没有在白板上写出这个题而遭到拒绝。
这个故事提醒我们算法很重要!
思路:交换节点+深搜即可。注意判断跟是否为空。
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null) return null;
if(root.left!=null&&root.right!=null){
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
else if(root.left!=null){
root.right=root.left;
root.left=null;
invertTree(root.right);
return root;
}
else if(root.right!=null){
root.left=root.right;
root.right=null;
invertTree(root.left);
return root;
}
else return root;
}
}