All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0501_0600.s0563_binary_tree_tilt.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0501_0600.s0563_binary_tree_tilt;

// #Easy #Depth_First_Search #Tree #Binary_Tree

import com_github_leetcode.TreeNode;

public class Solution {
    private int sum = 0;

    private int sumTilt(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int ls = sumTilt(root.left);
        int rs = sumTilt(root.right);
        sum += Math.abs(ls - rs);
        return ls + rs + root.val;
    }

    public int findTilt(TreeNode root) {
        sum = 0;
        sumTilt(root);
        return sum;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy