Leetcode563.Binary Tree Tilt二叉树的坡度
给定一个二叉树,计算整个树的坡度。
一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值。空结点的的坡度是0。
整个树的坡度就是其所有节点的坡度之和。
示例:
输入:
1
/ \
2 3
输出: 1
解释:
结点的坡度 2 : 0
结点的坡度 3 : 0
结点的坡度 1 : |2-3| = 1
树的坡度 : 0 + 0 + 1 = 1
注意:
- 任何子树的结点的和不会超过32位整数的范围。
- 坡度的值不会超过32位整数的范围。
class Solution {
public:
int findTilt(TreeNode* root) {
if(root == NULL)
return 0;
return abs(GetSum(root ->left) - GetSum(root ->right)) + findTilt(root ->left) + findTilt(root ->right);
}
int GetSum(TreeNode* root)
{
if(root == NULL)
return 0;
return root ->val + GetSum(root ->left) + GetSum(root ->right);
}
};
Leetcode563.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) 38
563. 二叉树的坡度 563. Binary Tree Tilt 题目描述 给定一个二叉树,计算整个树的坡度. 一个树的节点的坡度定义即为,该节点左子树的结点之和和右子树结点之和的差的绝对值.空结点 ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- [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 ...
- 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] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
随机推荐
- 第三周课堂笔记1thand2thand3th
元组 元组是以逗号隔开的 元组有索引有切片,元组是小括号和中括号的集合, 元组中的东西不可修改(小括号内的东西不可被修改,但是小括号里的列表和字典可以被修改) 2. 由内存地址来分 可变数据类 ...
- css中字体属性的简写
- Python系统(os)相关操作
文件操作 python中常用于文件处理的模块有os,shutil等. 1 创建文件 文件的创建可以使用open()函数,如下创建一个test_file.txt的文件: >>> wit ...
- springboot2.0 使用aop实现PageHelper分页
参考: https://blog.csdn.net/qq_24076135/article/details/85212081 https://www.jianshu.com/p/036d31ae77d ...
- html--设置复选按钮和单选按钮
- 2016.9.10初中部上午NOIP普及组比赛总结
2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...
- vue如何关闭sourceMap
Vue打包后出现一些map文件的解决办法: 问题: 可能很多人在做vue项目打包,打包之后js中,会自动生成一些map文件,那我们怎么把它去掉不要呢? 1,运行 cnpm run build 开始 ...
- js 验证图片
var selectedImg = e.target.files[0]; //获取图片 var isPic = /^(image\/bmp|image\/gif|image\/jpeg|image\/ ...
- 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)[转]
上篇文章<深入浅出Mybatis系列(一)---Mybatis入门>, 写了一个Demo简单体现了一下Mybatis的流程.本次,将简单介绍一下Mybatis的配置文件: 上次例子中,我们 ...
- IT技术博客
博客收藏大全: 陈皓博客: 陈硕的博客: 风云的博客: 当然我在扯淡: hellogirl: 田守枝Java技术博客: 廖雪峰博客: Milo游戏开发: