File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed
solution/0226.Invert Binary Tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments