|
5 | 5 | import java.util.LinkedList;
|
6 | 6 | import java.util.Queue;
|
7 | 7 |
|
8 |
| -/** |
9 |
| - * 226. Invert Binary Tree |
10 |
| -
|
11 |
| - Invert a binary tree. |
12 |
| -
|
13 |
| - 4 |
14 |
| - / \ |
15 |
| - 2 7 |
16 |
| - / \ / \ |
17 |
| - 1 3 6 9 |
18 |
| -
|
19 |
| - to |
20 |
| -
|
21 |
| - 4 |
22 |
| - / \ |
23 |
| - 7 2 |
24 |
| - / \ / \ |
25 |
| - 9 6 3 1 |
26 |
| -
|
27 |
| -Trivia: |
28 |
| -This problem was inspired by this original tweet by Max Howell: |
29 |
| -Google: 90% of our engineers use the software you wrote (Homebrew), |
30 |
| - but you can�t invert a binary tree on a whiteboard so fuck off. |
31 |
| - */ |
32 | 8 | public class _226 {
|
33 | 9 |
|
34 |
| - public static class Solution1 { |
35 |
| - public TreeNode invertTree(TreeNode root) { |
36 |
| - if (root == null) { |
37 |
| - return root; |
38 |
| - } |
39 |
| - Queue<TreeNode> q = new LinkedList(); |
40 |
| - q.offer(root); |
41 |
| - while (!q.isEmpty()) { |
42 |
| - TreeNode curr = q.poll(); |
43 |
| - TreeNode temp = curr.left; |
44 |
| - curr.left = curr.right; |
45 |
| - curr.right = temp; |
46 |
| - if (curr.left != null) { |
47 |
| - q.offer(curr.left); |
48 |
| - } |
49 |
| - if (curr.right != null) { |
50 |
| - q.offer(curr.right); |
51 |
| - } |
52 |
| - } |
53 |
| - return root; |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - public static class Solution2 { |
58 |
| - public TreeNode invertTree(TreeNode root) { |
59 |
| - if (root == null) { |
60 |
| - return root; |
61 |
| - } |
62 |
| - TreeNode temp = root.left; |
63 |
| - root.left = root.right; |
64 |
| - root.right = temp; |
65 |
| - invertTree(root.left); |
66 |
| - invertTree(root.right); |
67 |
| - return root; |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - public static class Solution3 { |
72 |
| - //more concise version |
73 |
| - public TreeNode invertTree(TreeNode root) { |
74 |
| - if (root == null) { |
75 |
| - return root; |
76 |
| - } |
77 |
| - TreeNode temp = root.left; |
78 |
| - root.left = invertTree(root.right); |
79 |
| - root.right = invertTree(temp); |
80 |
| - return root; |
81 |
| - } |
82 |
| - } |
| 10 | + public static class Solution1 { |
| 11 | + public TreeNode invertTree(TreeNode root) { |
| 12 | + if (root == null) { |
| 13 | + return root; |
| 14 | + } |
| 15 | + Queue<TreeNode> q = new LinkedList(); |
| 16 | + q.offer(root); |
| 17 | + while (!q.isEmpty()) { |
| 18 | + TreeNode curr = q.poll(); |
| 19 | + TreeNode temp = curr.left; |
| 20 | + curr.left = curr.right; |
| 21 | + curr.right = temp; |
| 22 | + if (curr.left != null) { |
| 23 | + q.offer(curr.left); |
| 24 | + } |
| 25 | + if (curr.right != null) { |
| 26 | + q.offer(curr.right); |
| 27 | + } |
| 28 | + } |
| 29 | + return root; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static class Solution2 { |
| 34 | + public TreeNode invertTree(TreeNode root) { |
| 35 | + if (root == null) { |
| 36 | + return root; |
| 37 | + } |
| 38 | + TreeNode temp = root.left; |
| 39 | + root.left = root.right; |
| 40 | + root.right = temp; |
| 41 | + invertTree(root.left); |
| 42 | + invertTree(root.right); |
| 43 | + return root; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static class Solution3 { |
| 48 | + //more concise version |
| 49 | + public TreeNode invertTree(TreeNode root) { |
| 50 | + if (root == null) { |
| 51 | + return root; |
| 52 | + } |
| 53 | + TreeNode temp = root.left; |
| 54 | + root.left = invertTree(root.right); |
| 55 | + root.right = invertTree(temp); |
| 56 | + return root; |
| 57 | + } |
| 58 | + } |
83 | 59 | }
|
0 commit comments