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 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
Note:
- The sum of node values in any subtree won't exceed the range of 32-bit integer.
- All the tilt values won't exceed the range of 32-bit integer.
题目标签:Tree
这道题目给了我们一个二叉树,要我们求出二叉树的倾斜度。题目给了例子真的容易误导人。一开始以为只要求2个children的差值,其实是要求left 和 right 各自的总和,包括加上它们自己,left 和 right 两个总和值的差值。利用post order 遍历二叉树,post order 的顺序是 左 右 根。这样的话就可以求出根的sum,左和右都return了以后,加上它自己的值。但是这样的recursively call 每次返回的都是一个点的sum 总值。我们需要求的是差值的总和。所以需要另外设一个res, 每次把差值加入res。
Java Solution:
Runtime beats 92.40%
完成日期:06/30/2017
关键词:Tree
关键点:用post order去拿到每次left 和right 加上自己的总和;分清楚return的值并不是我们最终求的值,需要另外设置一个res,还有另一个help function
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution
- {
- int res = 0;
- public int findTilt(TreeNode root)
- {
- postOrder(root);
- return res;
- }
- public int postOrder(TreeNode node)
- {
- if(node == null)
- return 0;
- int left_sum = postOrder(node.left);
- int right_sum = postOrder(node.right);
- res += Math.abs(left_sum - right_sum);
- return left_sum + right_sum + node.val;
- }
- }
参考资料:
https://leetcode.com/problems/binary-tree-tilt/#/solutions
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 563. Binary Tree Tilt (二叉树的倾斜度)的更多相关文章
- [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 ...
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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
Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- [LeetCode] Print Binary Tree 打印二叉树
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- 563. Binary Tree Tilt
https://leetcode.com/problems/binary-tree-tilt/description/ 挺好的一个题目,审题不清的话很容易做错.主要是tilt of whole tre ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
随机推荐
- java: Multiple encodings set for module chunk test "GBK" will be used by compiler
IDEA 进行编译代码的时候,特别是新项目 特别容易出现 编码错误,但是 File-Encoding中设置的又没有问题,而且maven 是能打包的,就是用 idea 自带的 编译的时候 就会出现提示 ...
- 框架应用:Spring framework (四) - 事务管理
事务控制 事务是什么?事务控制? 事务这个词最早是在数据库中进行应用,讲的用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位. 事务的管理是指一个事务的开启,内容添加, ...
- Eclipse 版本选择
查看Eclipse的版本号: 1. 找到eclipse安装目录 2. 进入readme文件夹,打开readme_eclipse.html 3. readme_eclipse.html呈现的第二行即数字 ...
- 深入理解计算机系统chapter2
---恢复内容开始--- 整数表示: 反码和原码都会有正零和负零 有符号整数和无符号整数之间的转换 反之 扩展一个数字的位级表示 截断操作 无符号加法的益处 补码的加法 规格化的值:E=e-bias ...
- uvalive 3029 City Game
https://vjudge.net/problem/UVALive-3029 题意: 给出一个只含有F和R字母的矩阵,求出全部为F的面积最大的矩阵并且输出它的面积乘以3. 思路: 求面积最大的子矩阵 ...
- FZU 1919 -- K-way Merging sort(记忆化搜索)
题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...
- P60 2.6
import java.util.Scanner; public class Num { public static void main(String[] args) { Scanner input ...
- 常用硬件设备GUID
Class GUID Device Description CDROM 4D36E965-E325-11CE-BFC1-08002BE10318 CD/DVD/Blu-ray drives DiskD ...
- 实战之elasticsearch集群及filebeat server和logstash server
author:JevonWei 版权声明:原创作品 实战之elasticsearch集群及filebeat server和logstash server 环境 elasticsearch集群节点环境为 ...
- zoj1151 zoj1295 Word Reversal 字符串的简单处理
Word Reversal Time Limit: 2 Seconds Memory Limit:65536 KB For each list of words, output a line ...