Golang concise solution (bottom-up recursive)


  • 0

    Bottom-up recursive, whenever we calculate the sum of left children and right chidren, we update the tilt as well.

    func findTilt(root *TreeNode) int {
    	res := 0
    	getSum(root, &res)
    	return res
    }
    
    func getSum(node *TreeNode, tilt *int) int {
    	if node == nil {
    		return 0
    	}
    
    	lsum, rsum := getSum(node.Left, tilt), getSum(node.Right, tilt)
    	diff := lsum - rsum
    	if diff < 0 {
    		diff = -diff
    	}
    	*tilt += diff
    
    	return lsum + rsum + node.Val
    }
    

Log in to reply
 

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