[JAVA] dfs / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        int sum = 0;
        public int findTilt(TreeNode root) {
            searchSum(root);
            return sum;
        }
        
        public int searchSum(TreeNode root){
            if(root == null)
                return 0;
            
            int left = searchSum(root.left);
            int right = searchSum(root.right);
            sum += Math.abs(left-right);
            return left + right + root.val;
        }
    }
    

Log in to reply
 

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