Skip to content

Commit 67b5404

Browse files
authored
226. Invert Binary Tree
1 parent 83c9898 commit 67b5404

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

226.InvertBinaryTree.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
//https://leetcode.com/problems/invert-binary-tree/description/
12+
class Solution {
13+
fun invertTree(root: TreeNode?): TreeNode? {
14+
if(root == null) return null
15+
16+
// Swap left,right child
17+
val temp = root.left
18+
root.left = root.right
19+
root.right = temp
20+
21+
//recurssive
22+
invertTree(root.left)
23+
invertTree(root.right)
24+
return root
25+
}
26+
}

0 commit comments

Comments
 (0)