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 ...
随机推荐
- LeetCode(数据库)部门最高工资
), Salary int, DepartmentId int) )) Truncate table Employee ') ') ') ') Truncate table Department ', ...
- sass环境搭建之node-sass,ruby
该内容全部为搬运,感谢作者的分享~,附有原文链接. 使用ruby环境 SASS学习系列之(一)--------- SASS,SCSS环境搭建(Ruby) 使用node-sass SASS学习系列之(二 ...
- python中的assert
assert 2>3, ("错误")print("haha") 如果断言处的表达式是错误的话,会打印assert后面的提示,并且下面的语句就不会执行了. ...
- 字符串翻转(C++)
1.字符串原地翻转,"abc"->"cba": int str_reverse(string &str,int first,int last) { ...
- LeetCode 1026. Maximum Difference Between Node and Ancestor
原题链接在这里:https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/ 题目: Given the ro ...
- ThinkCMF框架任意内容包含
更多内容,欢迎关注微信公众号:信Yang安全,期待与您相遇. ThinkCMF是一款基于PHP+MYSQL开发的中文内容管理框架,底层采用ThinkPHP3.2.3构建.ThinkCMF提出灵活的应用 ...
- WinDbg的环境变量
有很多的环境变量,主要分为常规环境变量和内核模式环境变量.下面分别列出. 常规环境变量 下表列出了可在用户模式和内核模式调试的环境变量. 变量 含义 _NT_DEBUGGER_EXTENSION_PA ...
- 汇编语言中 cs, ds,ss 的区别
CS(Code Segment):代码段寄存器:DS(Data Segment):数据段寄存器:SS(Stack Segment):堆栈段寄存器:ES(Extra Segment):附加段寄存器.当一 ...
- 使用vue+mintui 开发省市区功能
做移动端的都知道 经常会有省市区这种三级联动的功能 今天研究了一下午~ 1.准备工作 vue+mintui+省市区的json数据 下载地址:https://github.com/chzm/addres ...
- vue关于keep-alive的小坑
在移动端里 少不了底部导航 在做底部导航的时候点击都会重复请求 我就使用了keep-alive来缓存 每次点击的时候走缓存 这里还有个用途就是当有列表的时候点进详情在返回可以保存之前的滚动记录 不会刷 ...