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 ...
随机推荐
- java nio和bio
理解同步/异步,阻塞/非阻塞:https://juejin.im/entry/598da7d16fb9a03c42431ed3 2:http://qindongliang.iteye.com/blog ...
- Web 漏洞分析与防御之 XSS(一)
原文地址:Web 漏洞分析与防御之 XSS(一) 博客地址:http://www.extlight.com 一.全称 跨站脚本攻击(Cross Site Scripting) 二.原理 通过在网站中的 ...
- erlang的格式化字符串
往pgsql里面写数据的时候,不能双引号,开始纠结的不行,用拼字符串的形式,后来发现可以格式化字符串,泪奔 data_format.erl -module(data_format). -export( ...
- Java-Runoob:Java 异常处理
ylbtech-Java-Runoob:Java 异常处理 1.返回顶部 1. Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少 ...
- 爬虫高性能相关(协程效率最高,IO密集型)
一背景常识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,采用串行的方式执行,只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调的是:串行并不意味着低 ...
- 【做题记录】USACO gold * 50(第一篇)
orz xhk 5/50 1597: [Usaco2008 Mar]土地购买 $ f[i]=min(f[j]+x[i]*y[j+1]) $ 然后斜率优化 1699: [Usaco2007 Jan]Ba ...
- Cobalt strike 第二节生成报告
0x00前言: 上一节我们说了怎么连接到服务器 0x01生成报告: 首先打开Cobalt Strike 点击Cobalt Strike -> Preferences Preferences Pe ...
- 使用CCNode作为容器容易踩的坑
Cocos2dx中CCNode经常作为一个父容器,里面装一些UI控件,最后组成一个复杂的自定义的UI控件,但是在使用别人的自定义控件和自己写自定义问题的时候会踩一些坑. 首先拿到一个自定义的UI控件一 ...
- MyBatis初识(通过小实例清晰认识MyBatis)
1.MyBatis框架: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- Elasticsearch-2.4.3的3节点安装(多种方式图文详解)(含 head、kopf、marvel、shield和watcher插件安装和使用)
前提: Elasticsearch-2.4.3的下载(图文详解) Elasticsearch-2.4.3的单节点安装(多种方式图文详解) 我这里,以192.168.80.10(HadoopMaster ...