DEV Community

hamza72x
hamza72x

Posted on • Updated on

[Grind 169] 6. Invert Binary Tree

Dedicated to mxcl, Creator of brew :)

Image description

Problem Link: https://leetcode.com/problems/invert-binary-tree/

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 nil
    }

    var temp = root.Left
    root.Left = root.Right
    root.Right = temp

    invertTree(root.Left)
    invertTree(root.Right)

    return root
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)