Skip to content

Commit 72c44f2

Browse files
refactor 226
1 parent 5b7b8b4 commit 72c44f2

File tree

1 file changed

+12
-4
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+12
-4
lines changed

src/main/java/com/fishercoder/solutions/_226.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
public class _226 {
99

1010
public static class Solution1 {
11+
/**
12+
* An iterative solution
13+
*/
1114
public TreeNode invertTree(TreeNode root) {
12-
if (root == null) {
15+
if (root == null || (root.left == null && root.right == null)) {
1316
return root;
1417
}
1518
Queue<TreeNode> q = new LinkedList();
@@ -31,8 +34,11 @@ public TreeNode invertTree(TreeNode root) {
3134
}
3235

3336
public static class Solution2 {
37+
/**
38+
* A recursive solution
39+
*/
3440
public TreeNode invertTree(TreeNode root) {
35-
if (root == null) {
41+
if (root == null || (root.left == null && root.right == null)) {
3642
return root;
3743
}
3844
TreeNode temp = root.left;
@@ -45,9 +51,11 @@ public TreeNode invertTree(TreeNode root) {
4551
}
4652

4753
public static class Solution3 {
48-
//more concise version
54+
/**
55+
* A more concise version
56+
*/
4957
public TreeNode invertTree(TreeNode root) {
50-
if (root == null) {
58+
if (root == null || (root.left == null && root.right == null)) {
5159
return root;
5260
}
5361
TreeNode temp = root.left;

0 commit comments

Comments
 (0)