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 题目讲解汇总(持续更新中...)

[LeetCode] Binary Tree Tilt 二叉树的坡度的更多相关文章

  1. Leetcode563.Binary Tree Tilt二叉树的坡度

    给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. 示例: 输入: 1 / ...

  2. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  3. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  4. LeetCode Binary Tree Tilt

    原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...

  5. LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38

    563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...

  6. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  7. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  8. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  9. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

随机推荐

  1. [poj3254]Corn Fields_状压dp

    Corn Fields poj3254 题目大意:给你一个n*m的地,每一块地可以种或不种,两块种过的地不能挨着,可以一块都不种,问所有的种地方案数. 注释:读入用0和1,1<=n,m<= ...

  2. php正则相关知识点

    关于正则,其实简单就是搜索和匹配.php,java,python等都是支持正则的,php正则兼容perl.好多同学觉得正则比较难,比较抽象,其实正则是非常简单的,主要是一个熟悉和反复练习的结果,还有一 ...

  3. 解决Oracle登录时出现无法处理服务名问题

    1.首先找到客户端的tnsnames.ora文件,打开看看里面有没有配置相应的服务器名,服务器名就是你的数据库名: 2.如果有相应的服务器名,那就检查一下配置信息是否错误,如果没有就添加: 3.配置信 ...

  4. python开发装饰器的应用

    python全栈开发-Day10 装饰器(闭合函数的应用场)   一. 装饰器 装饰器就是闭包函数的一种应用场景 什么是闭包函数?我们再来回忆一下: 闭包函数: 定义在函数内部的函数,并且该函数包含对 ...

  5. c# 实时监控数据库 SqlDependency

    http://blog.csdn.net/idays021/article/details/49661855 class Program { private static string _connSt ...

  6. 2018上C语言程序设计(高级)作业- 初步计划

    C语言程序设计(高级)36学时,每周4学时,共9周.主要学习指针.结构和文件三部分内容.整个课程作业计划如下: PTA和博客的使用指南 若第一次使用PTA和博客,请务必先把PTA的使用简介和教师如何在 ...

  7. php的数组的函数

    1.可以将一个二位数组转化成两个一维数组,没有指定键就是默认的索引 注意二位数组有几种类型,其中最常见的一种是外层循环是一个索引数组,然后内层是一个关联数组.这种通过便利第一层,然后第二层指定关联词就 ...

  8. DNS搜索过程

    以www.renyi.com为例 一:客户端首先检查本地HOST文件,是否有对应的IP地址,如果有,客户端直接访问,如果没有,则执行下一步. 二:客户端查看本地缓存信息,是否有对应的IP地址,如果有, ...

  9. Linux入门(2)_给初学者的建议

    1 学习Linux的注意事项 严格区分大小写(命令, 文件, 选项) Linux中所有内容以文件形式保存, 包括硬件 硬盘文件是/dev/sd[a-p] 光盘文件是/dev/sr0等 Linux不靠扩 ...

  10. 解决SoapFault (looks like we got no XML document)问题

    今天在调试项目的时候出现下面的错误信息: SoapFault looks like we got no XML document (D:\phpStudy\WWW\self.shop.xunmall. ...