[抄题]:

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的参数 就能实现自动记录

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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 子节点差的绝对值之和的更多相关文章

  1. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  2. 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 ...

  3. 【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 ...

  4. 563. Binary Tree Tilt

    https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...

  5. [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 ...

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

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

  7. hdu 3473 区间条件极值 - 区间 差的绝对值 之和的最小

    题目传送门//res tp hdu 目的 对长度为n的区间,给定q个子区间,求一x,使得区间内所有元素与x的差的绝对值之和最小. 多测. n 1e5 q 1e5 ai [1,1e9] (i∈[1,n] ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. (转)如何制作nodejs,npm “绿色”安装包

      摘自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=8625039&id=3817492       由于公司环境 ...

  2. 优化html标签

    借用Effective之名,开始写Effective系列,总结一些前端的心得. 有些人写页面会走向一个极端,几乎页面所有的标签都用div,究其原因,用div有很多好处,一个是div没有默认样式,不会有 ...

  3. win10下启动zkui

    zkui是一个开源的zookeeper可视化工具,现在看下我们怎么启动这个工具.首先下载源码(我把它放在E:\workspace): git clone https://github.com/Deem ...

  4. golang里面检测对象是否实现了接口的方法

    写法有点怪异,记一下吧 _, implemented := this.delegate.(IGenTcpServerDelegate) if implemented { this.delegate.G ...

  5. 【学徒日记】Unity 动画调用事件

    http://note.youdao.com/noteshare?id=a15f965fc57a0b25c87ee09388cf0f4a 具体内容看上面的链接. 1. 在脚本里写一个函数,它的参数只能 ...

  6. 为什么 JSON 接口的数据都要加双引号!!!不能用单引号

    原因是:Javascript 在很多时候会把 JSON 对象里面没有双引号包围的值,当做数值处理.比如: {"a":987654321} 这个 JSON 里头的变量 a,会被当做一 ...

  7. Vim编辑器基本操作学习(二)

    操作符+位移 x命令可以删除一个字符,4x可以删除4个字符. dw可以删除一个word,w事实上是向后移动一个word的命令:dw可以接上一个任意一个位移命令,它将删除从当前光标开始到位移终点处的文本 ...

  8. MySQL5.7.18,初始化完成登录,提示密码过

    初始化完成登录,提示密码过期 原因: 安装CentOs的时候,默认安装了mysql,并且版本与自己安装的mysql版本不一致,直接使用mysql -uroot -p'password'连接,默认调用的 ...

  9. 第十章 Secret & Configmap (中)

    10.3 在Pod中使用Secret 10.3.1 Volume方式 apiVersion: v1 kind: Pod metaata: name: mypod spec: containers: - ...

  10. 【洛谷】P1892 团伙(并查集)+ 求助

    题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...