Golang iterative solution 3ms

  • 1
    func upsideDownBinaryTree(root *TreeNode) *TreeNode {
        curr := root
        var next *TreeNode
        var temp *TreeNode
        var prev *TreeNode
        for curr != nil {
            next = curr.Left
            curr.Left = temp
            temp = curr.Right
            curr.Right = prev
            prev = curr
            curr = next
        return prev

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.