LeetCode题解之Binary Tree Tilt
1、题目描述

2、分析
利用递归实现。
3、代码
int findTilt(TreeNode* root) {
if (root == NULL)
return ;
int ans = ;
nodesTilt(root,ans);
return ans;
}
int nodesTilt(TreeNode *root, int & ans)
{
if (root == NULL)
return ;
int tiltleft = nodesTilt(root->left, ans);
int tiltright = nodesTilt(root->right, ans);
ans += abs(tiltleft - tiltright);
return tiltleft + tiltright + root->val;
}
LeetCode题解之Binary Tree Tilt的更多相关文章
- 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【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 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary ...
- [LeetCode 题解]: Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- LeetCode算法题-Binary Tree Tilt(Java实现)
这是悦乐书的第263次更新,第276篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第130题(顺位题号是563).给定二叉树,返回整棵树的倾斜度.树节点的倾斜被定义为所有 ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- LeetCode题解之Binary Tree Pruning
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(ro ...
- LeetCode题解Maximum Binary Tree
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) ...
随机推荐
- url最后的“/”是什么作用
多了个尾巴 有时候,当你尝试在地址栏输入https://123/demo的时候,会发现浏览器会重定向到https://123/demo/这个地址,也就是多了个/,发生了重定向.有图为证: 上面这个图是 ...
- Shell脚本 | 一键卸载安卓App
在平时工作的过程中,很多重复性内容可以通过运行脚本文件来代替.一次编写,就能带来很大的效率提升. 今天跟大家分享一个简单的 Shell 脚本,只有区区 20 行左右的代码. 因为有时候我们测试某个应用 ...
- css outline实践研究
outline具有和border很相似的属性,但多少又有些区别,就是因为这些区别才让它闪闪发光,先目睹一下. <style> div{ width:100px; height:100px; ...
- Solidity中如何判断mapping中某个键是否为空呢?
Solidity中如何判断mapping中某个键是否为空呢? 一.比较标准的做法是建立一个专门和value相关的结构体,用一个布尔型变量来看是否这个key所对应的value被赋过值 代码如下: pra ...
- linux firefox 快捷方式
.输入:cd /usr/share/applications .输入:vi firefox.desktop 在vi里面输入以下内容,然后保存并退出: [Desktop Entry] Name=Fire ...
- Spring 环境与profile(一)——超简用例
什么是profile,为什么需要profile? 在开发时,不同环境(开发.联调.预发.正式等)所需的配置不同导致,如果每改变一个环境就更改配置不但麻烦(修改代码.重新构建)而且容易出错.Spring ...
- es6学习笔记8--Map数据结构
Map Map结构的目的和基本用法 JavaScript的对象(Object),本质上是键值对的集合(Hash结构),但是只能用字符串当作键.这给它的使用带来了很大的限制. var data = {} ...
- git将本地项目发布到远端
如果本地有个项目myapp之前没在git上,想上传到git仓库保存,操作如下 1. 在gitee或者github上创建一个新仓库 仓库 2. 在控制台进入本地已有的项目文件夹下 cd myapp 3. ...
- 在ASP.NET MVC使用JavaScriptResult
本例中,我们尝试把javascript程序搬至控制器中去.更好地在控制器编写程序. 首先来看看原来的写法. 在SepController控制器,添加如下操作: public ActionResult ...
- 【ibatis】入门讲例
Ⅰ .Ibatis项目机构 打开资源包,可以看到里面有一个simple_exzample的文件夹,在MyEclipse8.5中新建一个JAVA项目,将刚才的文件夹中内容复制到项目SRC下,这样的话呢, ...