Skip to content

Commit 720398a

Browse files
committed
Invert Binary Tree
1 parent 3ac0528 commit 720398a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

invert-binary-tree/TonyKim9401.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// TC: O(n)
2+
// -> visit all nodes to invert
3+
// SC: O(n)
4+
// -> create all nodes again to exchange
5+
class Solution {
6+
public TreeNode invertTree(TreeNode root) {
7+
if (root == null) return null;
8+
invertTree(root.left);
9+
invertTree(root.right);
10+
TreeNode left = root.left;
11+
TreeNode right = root.right;
12+
root.left = right;
13+
root.right = left;
14+
return root;
15+
}
16+
}

0 commit comments

Comments
 (0)