Simple and Clean Go Solution Using postorder traversal


  • 0
    M
    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    
    var sum int
    func convertBST(root *TreeNode) *TreeNode {
        if root == nil {
            return nil
        }
        
        sum = 0
        traverse(root)
        return root
    }
    
    func traverse(root *TreeNode) {
        if root == nil {
            return
        }
        traverse(root.Right)
        root.Val += sum
        sum = root.Val
        traverse(root.Left)
    }
    

Log in to reply
 

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