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 ...
随机推荐
- HL7 Tools suite
HL7的官网有很多开源工具, 比如:RoseTree,V3Generator,RMIM Designer, Design Repository, V2 & V3 Mapping Tools等. ...
- 本地oracle的配置连接
oraclediver_name=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@205.168.1.125:1521:orclusernam ...
- elasticSearch Java Spring Data Api
1. BoolQueryBuilder qb=QueryBuilders. boolQuery(); qb.should(QueryBuilders.matchQuery("keyWord& ...
- 让输入的字符转义成html实体的方法
使用 htmlspecialchars() 函数,代码不会执行,因为会被保存为转义代码 总结测试方法: https://www.cnblogs.com/kaibindirver/p/10321448. ...
- java web 程序---猜数字游戏
思路:1.第一个是随机产生的数字,告诉我们去猜 cai.jsp 2.第二个是一个form表单,提交按钮后,将连接到验证页面 test1.jsp 3.第三个是比较猜的数和随机数.对了,提示再玩一次,不 ...
- 第十一章: Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
HDFS的体系架构 整个Hadoop的体系结构主要是通过HDFS来实现对分布式存储的底层支持,并通过MR来实现对分布式并行任务处理的程序支持. HDFS采用主从(Master/Slave)结构模型,一 ...
- HDU5336题解
解题思路 这题思路并不难,主要问题是,不太好编码实现(可能是本人练习不够吧),因为有个时间在里面,而且每个小水滴都同时流动,感觉好复杂的样子.比赛时,我首先想到的是DFS+时间流做参数,由于比赛时神经 ...
- PHP正则表达式详解
正则表达式定义 正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等. 列目录时, ...
- 28_java之mysql的CRUD
01数据库概念 * A: 什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进行增加,修改,删除及查询操作. * B: 什么是数据 ...
- C# 重构
重构是在编写代码后在不更改代码的外部行为的前提下通过更改代码的内部结构来改进代码的过程. 一.何时需要重构 1.代码中存在重复的代码: 如果类中有重复的代码块,需将其提炼出一个新的独立方法,如果是不同 ...