Easy and Concise Java Code


  • 0
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        private int maxDiff = 0;
        public int findTilt(TreeNode root) {
            if (root == null) return 0;
            helper(root);
            return maxDiff;
        }
        private int helper(TreeNode root) {
            if (root == null) return 0;
            int left = helper(root.left);
            int right = helper(root.right);
            maxDiff += 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.