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. RDD概念、特性、缓存策略与容错

    一.RDD概念与特性 1. RDD的概念 RDD(Resilient Distributed Dataset),是指弹性分布式数据集.数据集:Spark中的编程是基于RDD的,将原始数据加载到内存变成 ...

  2. centos7下搭建sentry错误日志服务器

    1. docker 安装(方法一) 1.确保yum packages 是最新的 $ sudo yum update 2.添加yum repo $ sudo tee /etc/yum.repos.d/d ...

  3. Linux学习--进程创建

    进程创建 在Linux系统下,自己可以创建进程: 当进程执行时,它会被装载进虚拟内存,为程序变量分配空间,并把相关信息添到 task_struct里. 进程内存布局分为四个不同的段: • 文本段,包含 ...

  4. DML数据操作语言之常用函数

    所谓函数,就是输入某一值,得到相应的输出结果的功能.相当于一个加工厂,给了原料,最终产出成品. 其中原料 就是参数(parameter). 产品 就是返回值. 函数大致可以分为以下五个种类: 算术函数 ...

  5. 一个页面多个HTTP请求 页面卡顿!

    用promise解决 前两天面试的时候 一个面试官问到这样一个问题 这里先说出解决的路径 这几天会更新具体的做法 或者直接参考廖雪峰大神 地址如下: https://www.liaoxuefeng.c ...

  6. linux的slect的脚本适用于交互

    [rhuang@localhost ~]$ vi os.sh #!/bin/bash echo "What is your favourite OS?" select var in ...

  7. Hangfire使用ApplicationInsigts监控

    起因 我司目前使用清真的ApplicationInsights(以下简称Ai)来做程序级监控.(Ai相关文档: https://azure.microsoft.com/zh-cn/services/a ...

  8. AngularJS1.X学习笔记9-自定义指令(中)

    今天好大的雨啊!上一节中,我们的指令中的工厂函数中都是返回了一个叫做链接函数的工人函数,事实上我们的工厂函数也是可以返回一个对象,这个对象里面可以包含很多的属性,这使得我们可以创建更加强大的指令. 一 ...

  9. BizTalk 2016 配置 RosettaNet遇到的坑

    本文只针对已经安装好BizTalk 2016 需要在安装RosettaNet加速器的伙伴. IIS配置 权限问题 错误信息 Failed to get IIS metabase property. E ...

  10. vue-cli项目中,全局引入jquery

    命令行执行 npm install --save jquery 找到webpack.base.conf.js文件,写入代码: const webpack = require('webpack') 在m ...