563. 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
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
理解DFS的返回值适用于所有点,ans[0]的返回值只适用于root一个点
[奇葩corner case]:
[思维问题]:
以为要用hashmap把每个点的距离差都存起来,但其实用traverse的参数 就能实现自动记录
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- DFS 的第一步别忘了写退出条件,树中是root == null
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
traverse(节点,ans[0]), 可以自动记录每个附带的值
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
DFS先退出:
public int dfs(TreeNode root, int[] ans) {
//exit
if (root == null) {
return 0;
}
//expand
int left = dfs(root.left, ans);
int right = dfs(root.right, ans);
ans[0] += Math.abs(left - right);
//return
return left + right + root.val;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int findTilt(TreeNode root) {
//corner case
if (root == null) {
return 0;
}
int[] ans = new int[1];
dfs(root, ans);
//return
return ans[0];
} public int dfs(TreeNode root, int[] ans) {
//exit
if (root == null) {
return 0;
}
//expand
int left = dfs(root.left, ans);
int right = dfs(root.right, ans); ans[0] += Math.abs(left - right);
//return
return left + right + root.val;
}
}
563. Binary Tree Tilt 子节点差的绝对值之和的更多相关文章
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- 【leetcode】563. Binary Tree Tilt
Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...
- 563. Binary Tree Tilt
https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38
563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...
- hdu 3473 区间条件极值 - 区间 差的绝对值 之和的最小
题目传送门//res tp hdu 目的 对长度为n的区间,给定q个子区间,求一x,使得区间内所有元素与x的差的绝对值之和最小. 多测. n 1e5 q 1e5 ai [1,1e9] (i∈[1,n] ...
- [LeetCode] Binary Tree Tilt 二叉树的坡度
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- [Swift]LeetCode563. 二叉树的坡度 | Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
随机推荐
- Ubuntu 14.04 配置安卓5.1编译环境
Ubuntu 14.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.android5.1需要安装openjdk- ...
- bootstrap常用模态框
<!-- 触发器(button) --> <button class="btn btn-xs btn-success" data-toggle="mod ...
- Data_Structure02-线性表
一.PTA实验作业 本周要求挑3道题目写设计思路.调试过程.设计思路用伪代码描述. 1.顺序表选择一题(6-2,6-3,7-1选一题),代码必须用顺序结构抽象数据类型封装 2.单链表选择一题(6-1不 ...
- (转)Inno Setup入门(六)——在程序目录下创建文件夹
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250789 创建文件夹可以使用[dirs]段实现,代码如下: [s ...
- ffmpeg 基本数据结构和对象: AVPacket、AVPicture、AVFrame
一.AVPacket /** * AVPacket 作为解码器的输入 或 编码器的输出. * 当作为解码器的输入时,它由demuxer生成,然后传递给解码器 * 当作为编码器的输出时,由编码器生成,然 ...
- 【转】jMeter入门实例
人们对从认识事物都有一个具体到抽象的过程,学习Jmeter也不例外,通过一个实例来进行学习,一方面可以让初学者有所见即所得的信心,另一方面,其实也是在初学者心中留下了对这事物的一个朦胧的印象,这在以后 ...
- SQLSERVER出错提示:此上下文中不允许使用''。此处只允许使用常量、表达式或变量。不允许使用列名。
在执行一段SQL语句时出现了这样的一段错误提示,在网上找了不少答案,都说的不是很详细,反复修改试验,最终解决了此问题.原SQl语句为: insert into shoufei(djbh,sflb,jk ...
- docker 学习(十一) 镜像常用命令
1 创建账户,创建仓库 首先在dockerhub上有自己的账户,然后创建一个repository(如上图), 然后创建一个名字为robinfei/consumer的仓库. 2 本地镜像打标签(比 ...
- Centos 6.5 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(2)
一.Centos 6.5 下的Zabbix Server安装 上篇文章记录的是centos 7 下安装zabbix ,很简单.但是6.5上面没有可用的源直接安装zabbix,所以需要从别处下载.感谢i ...
- IDA Pro 权威指南学习笔记(十) - 栈帧
栈帧(stack frame)是在程序的运行时栈中分配的内存块,用于特定的函数调用 如果一个函数没有执行则不需要内存,当函数被调用时就需要用到内存 1.传给函数的参数的值需要存储到函数能够找到它们的位 ...