Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 739 Bytes

Question_563.md

File metadata and controls

31 lines (25 loc) · 739 Bytes

LeetCode Records - Question 563 Binary Tree Tilt

Attempt 1: Use recursion

class Solution {

    private int sum = 0;

    public int findTilt(TreeNode root) {
        findTiltRecursion(root);
        return sum;
    }

    private int findTiltRecursion(TreeNode root) {
        if (root == null) {
            return 0;
        } else if (root.left == null && root.right == null) {
            return root.val;
        }

        int leftVal = findTiltRecursion(root.left);
        int rightVal = findTiltRecursion(root.right);
        sum += Math.abs(leftVal - rightVal);
        return leftVal + rightVal + root.val;
    }
}
  • Runtime: 0 ms (Beats: 100.00%)
  • Memory: 44.19 MB (Beats: 53.25%)