File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change 8
8
public class _226 {
9
9
10
10
public static class Solution1 {
11
+ /**
12
+ * An iterative solution
13
+ */
11
14
public TreeNode invertTree (TreeNode root ) {
12
- if (root == null ) {
15
+ if (root == null || ( root . left == null && root . right == null ) ) {
13
16
return root ;
14
17
}
15
18
Queue <TreeNode > q = new LinkedList ();
@@ -31,8 +34,11 @@ public TreeNode invertTree(TreeNode root) {
31
34
}
32
35
33
36
public static class Solution2 {
37
+ /**
38
+ * A recursive solution
39
+ */
34
40
public TreeNode invertTree (TreeNode root ) {
35
- if (root == null ) {
41
+ if (root == null || ( root . left == null && root . right == null ) ) {
36
42
return root ;
37
43
}
38
44
TreeNode temp = root .left ;
@@ -45,9 +51,11 @@ public TreeNode invertTree(TreeNode root) {
45
51
}
46
52
47
53
public static class Solution3 {
48
- //more concise version
54
+ /**
55
+ * A more concise version
56
+ */
49
57
public TreeNode invertTree (TreeNode root ) {
50
- if (root == null ) {
58
+ if (root == null || ( root . left == null && root . right == null ) ) {
51
59
return root ;
52
60
}
53
61
TreeNode temp = root .left ;
You can’t perform that action at this time.
0 commit comments