Skip to content

Commit b051823

Browse files
committed
Add Solution.java for 0226.Invert Binary Tree
1 parent 4184805 commit b051823

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Invert Binary Tree
2+
3+
Invert a binary tree.
4+
5+
## Example:
6+
```
7+
Input:
8+
9+
4
10+
/ \
11+
2 7
12+
/ \ / \
13+
1 3 6 9
14+
Output:
15+
16+
4
17+
/ \
18+
7 2
19+
/ \ / \
20+
9 6 3 1
21+
```
22+
23+
##Trivia:
24+
This problem was inspired by this original tweet by Max Howell:
25+
26+
```
27+
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
28+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode invertTree(TreeNode root) {
12+
if (root == null) {
13+
return null;
14+
}
15+
TreeNode right = invertTree(root.right);
16+
TreeNode left = invertTree(root.left);
17+
root.left = right;
18+
root.right = left;
19+
return root;
20+
}
21+
}

0 commit comments

Comments
 (0)