226. Invert Binary Tree

🟩 Easy | 🟧 Medium | 🟥 Hard

Solution

My Solution

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return root
    }
    
    if root.Left != nil {
        root.Left = invertTree(root.Left)
    }

    if root.Right != nil {
        root.Right = invertTree(root.Right)
    }

    root.Left, root.Right = root.Right, root.Left

    return root
}
result

Leetcode: link

Last updated

Was this helpful?