Golang solution with clear code


  • 0
    B
    func findTilt(root *TreeNode) int {
    	if root == nil {
    		return 0
    	}
    	first, _ := dfs(root)
    	return first
    }
    
    func dfs(root *TreeNode) (int, int) { // return tilt and sum of root
    	if root == nil {
    		return 0, 0
    	}
    	lt, ls := dfs(root.Left)  // left tilt, left sum
    	rt, rs := dfs(root.Right) // right tilt, right sum
    	return lt + rt + abs(ls, rs), root.Val + ls + rs
    }
    

Log in to reply
 

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