LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38
563. 二叉树的坡度
563. Binary Tree Tilt
题目描述
给定一个二叉树,计算整个树的坡度。
一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是 0。
整个树的坡度就是其所有节点的坡度之和。
每日一算法2019/6/10Day 38LeetCode563. Binary Tree Tilt
示例:
输入:
1
/ \
2 3
输出: 1
解释:
结点的坡度 2 : 0
结点的坡度 3 : 0
结点的坡度 1 : |2-3| = 1
树的坡度 : 0 + 0 + 1 = 1
注意:
- 任何子树的结点的和不会超过 32 位整数的范围。
- 坡度的值不会超过 32 位整数的范围。
Java 实现
TreeNode Class
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public int findTilt(TreeNode root) {
if (root == null) {
return 0;
}
int[] res = new int[1];
postorder(root, res);
return res[0];
}
public int postorder(TreeNode root, int[] res) {
if (root == null) {
return 0;
}
int leftSum = postorder(root.left, res);
int rightSum = postorder(root.right, res);
res[0] += Math.abs(leftSum - rightSum);
return leftSum + rightSum + root.val;
}
}
参考资料
- https://leetcode-cn.com/problems/binary-tree-tilt/
- https://leetcode.com/problems/binary-tree-tilt/
- https://www.cnblogs.com/grandyang/p/6786643.html
LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38的更多相关文章
- [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实现 LeetCode 563 二叉树的坡度(又是一个遍历树)
563. 二叉树的坡度 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点的的坡度是0. 整个树的坡度就是其所有节点的坡度之和. ...
- [Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
[题目] Given a binary tree, return the preordertraversal of its nodes' values. Example: Input: [1,null ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)
Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...
- 笔试算法题(41):线索二叉树(Threaded Binary Tree)
议题:线索二叉树(Threaded Binary Tree) 分析: 为除第一个节点外的每个节点添加一个指向其前驱节点的指针,为除最后一个节点外的每个节点添加一个指向其后续节点的指针,通过这些额外的指 ...
- [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 ...
随机推荐
- django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: Course has no coursedetail.
错误描述: 一对一反向查询失败! 前提: Course和CourseDetail OneToOne 原因: Course数据和CourseDetail数据没有一一对应.
- ACM-ICPC 2018 南京赛区现场赛 K. Kangaroo Puzzle (思维+构造)
题目链接:https://codeforc.es/gym/101981/attachments 题意:在 n * m 的平面上有若干个袋鼠和墙(1为袋鼠,0为墙),每次可以把所有袋鼠整体往一个方向移动 ...
- ES6函数的个人总结
默认参数: 1. 在 ES5 语法中,为函数形参指定默认值的写法: 写法一: function foo (bar) { bar = bar || 'abc'; console.log(bar) } f ...
- WinDbg常用命令系列---!cppexr
!cppexr 简介 !cppexr显示C++异常记录的内容. 使用形式 !cppexr Address 参数 Address指定要显示的C++异常记录的地址. 支持环境 Windows 2000 E ...
- Windbg命令的语法规则系列(二)
二.字符串通配符语法 一些调试器命令具有接受各种通配符的字符串参数.这些类型的参数支持以下语法功能: 星号(*)表示零个或多个字符. 问号(?)表示任何单个字符. 包含字符列表的括号([])表示列表中 ...
- div+css制作哆啦A梦
纯CSS代码加上 制作动画版哆啦A梦(机器猫) 哆啦A梦(机器猫)我们大家一定都很熟悉,今天给大家演示怎么用纯CSS.代码,来做一个动画版的哆啦A梦. 效果图: 下面代码同学可以查看一下,每个线条及椭 ...
- jaeger 使用scylladb作为后端存储
scylladb 是一个不错的apache Cassandra 替代,而且兼容很不错,今天在尝试过yugabyte 之后放弃了,因为在进行jaeger 创建 Cassandra schema 的时候碰 ...
- NOI 2019 游记
day -1 去报了个到,顺便买了一大堆衣服. 感觉学校饭堂不太行. day 0 上午是开幕式,. 下午是笔试,顺利获得 \(100\) 分. day 1 先看题. 第一题看到 \(At^2+Bt+C ...
- 洛谷P1577 切绳子题解
洛谷P1577 切绳子题解 题目描述 有N条绳子,它们的长度分别为Li.如果从它们中切割出K条长度相同的 绳子,这K条绳子每条最长能有多长?答案保留到小数点后2位(直接舍掉2为后的小数). 输入输出格 ...
- 均值不等式中的一则题目$\scriptsize\text{$(a+\cfrac{1}{a})^2+(b+\cfrac{1}{b})^2\ge \cfrac{25}{2}$}$
例题已知正数\(a.b\)满足条件\(a+b=1\),求\((a+\cfrac{1}{a})^2+(b+\cfrac{1}{b})^2\)的最小值: 易错方法\((a+\cfrac{1}{a})^2+ ...