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 ...
随机推荐
- python读取外部文件
>>> pd.read_excel('c://111.xlsx') 年度排名 历史排名 电影名称 总票房 总人次 总场次 上映年份 操作 0 1 1 美人鱼 NaN -- -- 20 ...
- 工作总结--如何定位web系统前后台的bug,以及bug分析/测试感想
对于web项目前台和后台bug定位分析:一. 系统整体了解 懒企鹅营销服务平台用的架构:web前端: Bootstrap 3.0 组件丰富,兼容性好,界面美观 Server端: jsp+Servlet ...
- python实例编写(2)--等待,一组对象,层级元素,frame对象处理
一.设置等待 #coding=utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDri ...
- IIS部署新网站
Windows Server使用IIS 6.0配置ASP动态Web网站 http://jingyan.baidu.com/article/c1a3101ee43ae9de656debb4.html h ...
- 尝试在Linux上部署Asp.net Core应用程序
快两个月没接触.net,倒是天天在用Linux,所以想尝试一下在Linux运行喜欢的.net 应用. 安装CentOS 安装.Net core for Linux 创建Asp.net Core应用程序 ...
- Docker入门之三容器
上一篇博客学习了下镜像,今天来学习容器.容器类似一个手机中的沙盒环境,用来运行app实例.和镜像一样也是对容器的创建.删除.导出等. 由于我买的参考书中的例子好多都是基于linux的,所以我将dock ...
- PyTorch教程之Neural Networks
我们可以通过torch.nn package构建神经网络. 现在我们已经了解了autograd,nn基于autograd来定义模型并对他们有所区分. 一个 nn.Module模块由如下部分构成:若干层 ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- 【maven插件】maven-shade-plugin
概述 该插件提供了将artifact打包到一个本地jar包的能力,包括其依赖关系以及一些参数如 shade -rename重命名依赖关系的包. 目标 shade:shade 绑定到建生命周期中的pac ...
- ZOJ2345Gold Coins 简单分块
昨天做过一样的题: 平方和公式:n*(n+1)*(2n+1) #include<cstdio> #include<cstdlib> #include<iostream&g ...