Java "global variable" solution


  • 0
    public class Solution {
        int tilt;
        public int findTilt(TreeNode root) {
            tilt = 0;
            getSum(root);
            return tilt;
        }
        
        private int getSum(TreeNode node) {
            if (node == null) {
                return 0;
            }
            int leftSum = getSum(node.left);
            int rightSum = getSum(node.right);
            tilt += Math.abs(leftSum - rightSum);
            return leftSum + rightSum + node.val;
        }
    }

Log in to reply
 

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