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.

这道题让我们求二叉树的坡度,某个结点的坡度的定义为该结点的左子树之和与右子树之和的差的绝对值,这道题让我们求所有结点的坡度之和。我开始的想法就是老老实实的按定义去做,用先序遍历,对于每个遍历到的结点,先计算坡度,根据定义就是左子树之和与右子树之和的差的绝对值,然后返回的是当前结点的tilt加上对其左右子结点调用求坡度的递归函数即可。其中求子树之和用另外一个函数来求,也是用先序遍历来求结点之和,为了避免重复运算,这里用哈希表来保存已经算过的结点,参见代码如下:

解法一:

class Solution {
public:
unordered_map<TreeNode*, int> m;
int findTilt(TreeNode* root) {
if (!root) return ;
int tilt = abs(getSum(root->left, m) - getSum(root->right, m));
return tilt + findTilt(root->left) + findTilt(root->right);
}
int getSum(TreeNode* node, unordered_map<TreeNode*, int>& m) {
if (!node) return ;
if (m.count(node)) return m[node];
return m[node] = getSum(node->left, m) + getSum(node->right, m) + node->val;
}
};

但是在论坛中看了大神们的帖子后,发现这道题最好的解法应该是用后序遍历来做,因为后序遍历的顺序是左-右-根,那么就会从叶结点开始处理,这样我们就能很方便的计算结点的累加和,同时也可以很容易的根据子树和来计算tilt,参见代码如下:

解法二:

class Solution {
public:
int findTilt(TreeNode* root) {
int res = ;
postorder(root, res);
return res;
}
int postorder(TreeNode* node, int& res) {
if (!node) return ;
int leftSum = postorder(node->left, res);
int rightSum = postorder(node->right, res);
res += abs(leftSum - rightSum);
return leftSum + rightSum + node->val;
}
};

参考资料:

https://discuss.leetcode.com/topic/87191/java-o-n-postorder-traversal

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. App-Pass the password
  2. Power Network(网络流最大流 &amp; dinic算法 + 优化)
  3. k-means
  4. vector的插入、lower_bound、upper_bound、equal_range实例
  5. javascript AOP实现
  6. Linux 网络设备驱动程序设计(2)
  7. Linux 性能监控的18个命令行工具
  8. 常见CSS注意问题
  9. STL之deque双向队列
  10. stl1
  11. Android项目开发填坑记-9patchPng报错
  12. CMDB服务器管理系统【s5day89】:采集资产之汇报信息
  13. 【Java集合系列四】HashSet和LinkedHashSet解析
  14. 独家!DevExpress VCL Controls 2019发展路线图(No.1)
  15. Legal or Not ,图的拓扑
  16. python---用链表结构实现有序和无序列表的几个功能
  17. 【PyTorch深度学习60分钟快速入门 】Part1:PyTorch是什么?
  18. Photoshop 基础六 图层
  19. Python之旅:MySQL系列
  20. Redis学习十:Redis的复制(Master/Slave)【重要】

热门文章

  1. Spring中配置数据源常用的形式
  2. Django学习(六)---博客文章页面的超链接设置
  3. fetch()函数使用的一些技巧
  4. beta版本复审
  5. 第一周-JAVA基本概念
  6. day-7 一个简单的决策树归纳算法(ID3)python编程实现
  7. Node入门教程(1)目录
  8. Java8-如何构建一个Stream
  9. c# aynsc 和 await
  10. Python内置函数(33)——any