Java clean solution


  • 0
    E
        
        public int findTilt(TreeNode root) {
            if(root == null) return 0;
            
            return findTilt(root.left) + findTilt(root.right) + Math.abs(nodeSum(root.left) - nodeSum(root.right));
        
        }
        
        //nodeSum is defined as the the sum of all the node's children and the node itself
        public int nodeSum(TreeNode node){
            if(node == null) return 0;
            return node.val + nodeSum(node.left) + nodeSum(node.right);
        }
    }
    

Log in to reply
 

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