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 ...
随机推荐
- demo 2 chart 报表
function killerrors() { return true; } window.onerror = killerrors; //检查浏览器类型 function checkBrowser( ...
- php端安装rabbitmq-c
php端安装rabbitmq-c url:https://github.com/alanxz/rabbitmq-c cd rabbitmq-c**** ./configure --prefix=/us ...
- (转)Inno Setup入门(八)——有选择性的安装文件
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250827 这主要使用[Components]段实现,一个演示的代 ...
- python--logging库学习_第一波
简单使用 #!/usr/local/bin/python # -*- coding:utf-8 -*- import logging logging.debug('debug message') lo ...
- java6枚举类型
java.lang.Enum > 使用enum定义. 类如: public class EnumDemo { enum Edge { TOP, BOTTOM, LEFT, RIGHT//定义了一 ...
- 杂项:NoSQL
ylbtech-杂项:NoSQL NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站 ...
- 0908期 HTML form表单
表单基础摘要 <form id="" name="" method="post/get" action="负责处理的服务端& ...
- 快速安装laravel和依赖
http://pkg.phpcomposer.com CMD敲命令: composer config -g repositories.packagist composer http://packagi ...
- C#枚举(enum)、常量(const)和readonly
const修饰的是(类)静态常量,,其值是在编译期间确定的readonly修饰的是动态常量. A.C#中的const和readonly的区别 C#中定义常量有两种方式,一种叫做静态常量,使用“cons ...
- App切图命名规范
转自:http://www.uisdc.com/slice-rename-in-ui-design(略有修改) 关于切图命名的规范,我个人觉得关键是在于团队能够有一个统一的规则,所有成员严格遵守并且和 ...