Leetcode-Solutions

My Leetcode Solutions.

View on GitHub

226. Invert Binary Tree

Topics: Tree DFS BFS Binary Tree

Solution

Implementation

class Solution {
    fun invertTree(root: TreeNode?): TreeNode? {
        if (root != null) {
            val newLeft = invertTree(root.right)
            val newRight = invertTree(root.left)
            root.apply {
                left = newLeft
                right = newRight
            }
        }
        return root
    }
}