• Binary Tree Tilt


    Given a binary tree, return the tilt of the whole tree.

    The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.

    The tilt of the whole tree is defined as the sum of all nodes' tilt.

    Example:

    Input: 
             1
           /   
          2     3
    Output: 1
    Explanation: 
    Tilt of node 2 : 0
    Tilt of node 3 : 0
    Tilt of node 1 : |2-3| = 1
    Tilt of binary tree : 0 + 0 + 1 = 1
    

    Note:

    1. The sum of node values in any subtree won't exceed the range of 32-bit integer.
    2. All the tilt values won't exceed the range of 32-bit integer.
     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     private int tilt = 0;
    12     
    13     public int findTilt(TreeNode root) {
    14         subtreeSum(root);
    15         return tilt;
    16     }
    17     
    18     private int subtreeSum(TreeNode root) {
    19         if (root == null) return 0;
    20         if (root.left == null && root.right == null) return root.val;
    21         
    22         int leftSum = subtreeSum(root.left);
    23         int rightSum = subtreeSum(root.right);
    24         tilt += Math.abs(leftSum - rightSum);
    25         return root.val + leftSum + rightSum;
    26     }
    27 }
     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public int findTilt(TreeNode root) {
    12         if (root == null) return 0;
    13         if (root.left == null && root.right == null) return 0;
    14         
    15         int myTilt = findNodeTilt(root);
    16         return myTilt + findTilt(root.left) + findTilt(root.right);
    17     }
    18     
    19     public int findNodeTilt(TreeNode root) {
    20         if (root == null) return 0;
    21         if (root.left == null && root.right == null) return 0;
    22         int leftSum = subtreeSum(root.left);
    23         int rightSum = subtreeSum(root.right);
    24         
    25         return Math.abs(leftSum - rightSum);
    26     }
    27     
    28     private int subtreeSum(TreeNode root) {
    29         if (root == null) return 0;
    30         if (root.left == null && root.right == null) return root.val;
    31         
    32         return root.val + subtreeSum(root.left) + subtreeSum(root.right);
    33     }
    34 }
     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     private HashMap<TreeNode, Integer> treeSum;
    12     
    13     public int findTilt(TreeNode root) {
    14         treeSum = new HashMap<TreeNode, Integer>();
    15         subtreeSum(root);
    16         return findTiltHelper(root);
    17     }
    18     
    19     private int findTiltHelper(TreeNode root) {
    20         if (root == null) return 0;
    21         if (root.left == null && root.right == null) return 0;
    22         
    23         int myTilt = findNodeTilt(root);
    24         return myTilt + findTiltHelper(root.left) + findTiltHelper(root.right);
    25     }
    26     
    27     public int findNodeTilt(TreeNode root) {
    28         if (root == null) return 0;
    29         if (root.left == null && root.right == null) return 0;
    30         int leftSum = treeSum.containsKey(root.left) ? treeSum.get(root.left) : 0;
    31         int rightSum = treeSum.containsKey(root.right) ? treeSum.get(root.right) : 0;
    32         
    33         return Math.abs(leftSum - rightSum);
    34     }
    35     
    36     private int subtreeSum(TreeNode root) {
    37         if (root == null) {
    38             treeSum.put(root, 0);
    39             return 0;
    40         }
    41         if (root.left == null && root.right == null) {
    42             treeSum.put(root, root.val);
    43             return root.val;
    44         }
    45         if (treeSum.containsKey(root)) return treeSum.get(root);
    46         
    47         int leftSum = subtreeSum(root.left);
    48         int rightSum = subtreeSum(root.right);
    49         int sum = root.val + leftSum + rightSum;
    50         treeSum.put(root, sum);
    51         return sum;
    52     }
    53 }
  • 相关阅读:
    MySQL存储引擎与索引
    最长公共子序列
    最长递增子序列
    排序算法
    二分查找及其变种
    多线程中锁的种类。
    <LeetCode>136. 只出现一次的数字
    <LeetCode>121. 买卖股票的最佳时机
    Netty 粘包/拆包应用案例及解决方案分析
    微服务
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6791732.html
Copyright © 2020-2023  润新知