563. Binary Tree Tilt
https://leetcode.com/problems/binary-tree-tilt/description/
挺好的一个题目,审题不清的话很容易做错。主要是tilt of whole tree 的定义是sum of all node's tilt 而不是想当然的tilt of root.
一开是我就以为是简单的tilt of root 导致完全错误。思路其实可以看作是求sum of children 的变种,只是这里不仅要跟踪每个子树的sum 还要累计上他们的tilt。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int tiltIter(TreeNode* root, int *t) {
if (root == nullptr) {
return ;
} int leftSum = ;
int rightSum = ;
if (root->left != nullptr) {
leftSum = tiltIter(root->left, t);
}
if (root->right != nullptr) {
rightSum = tiltIter(root->right, t);
}
//tilt of the current node;
int tilt = abs(leftSum - rightSum);
*t += tilt;
return root->val + leftSum + rightSum;
} int findTilt(TreeNode* root) {
int tilt = ;
tiltIter(root, &tilt);
return tilt;
}
};
563. Binary Tree Tilt的更多相关文章
- 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&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 ...
- 563. Binary Tree Tilt 子节点差的绝对值之和
[抄题]: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as ...
- 【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】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- LeetCode 563. 二叉树的坡度(Binary Tree Tilt) 38
563. 二叉树的坡度 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 ...
- [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 Binary Tree Tilt
原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...
随机推荐
- jemter+ant+jenkins进行集成测试
一下为我学习的一些笔记: 一.安装配置ant 安装地址:http://ant.apache.org/ 1.下载ant一路傻瓜式安装 2.配置ant环境变量:path下配置ant的bin路径 3.将jm ...
- 人工智能初识(百度ai)
目前的人工智能做了什么? 语音识别:小米的小爱同学,苹果的siri,微软的Cortana语音合成:小米的小爱同学,苹果的siri,微软的Cortana图像识别:交通摄像头拍违章,刷脸解锁手机等视频识别 ...
- JavaScript数据类型检测 数组(Array)检测方式
前言 对于确定某个对象是不是数组,一直是数组的一个经典问题.本文专门将该问题择出来,介绍什么才是正确的javascript数组检测方式 typeof 首先,使用最常用的类型检测工具--typeof运算 ...
- arcgis图层 GraphicsLayer与FeatureLayer
什么是图层 图层是用来在 ArcGIS 产品套件中显示地理数据集的机制.每个图层代表一种数据集(可以是地图服务.图形或是矢量数据),并指定该数据集是如何描绘使用一组属性的. 包含一个地图控件的每个应用 ...
- 【C++笔记】析构函数(destructor)
“析构函数”是构造函数的反向函数. 在销毁(释放)对象时将调用它们. 通过在类名前面放置一个波形符 (~) 将函数指定为类的析构函数. 声明析构函数 析构函数是具有与类相同的名称但前面是波形符 ...
- git出现refusing to merge unrelated histories
问题描述当本地分支与远程分支没有共同祖先时,会出现 fatal: refusing to merge unrelated histories 的问题. 解决方案可以使用 rebase 的方式来进行合并 ...
- centos 7.2下搭建vsftp 虚拟用户
虚拟用户搭建vsftp 要求一: 只允许上传 下载 不能删除 不能更换名称 yum install pam* yum install db4* -y yum install vsftpd chkcon ...
- 如何解压DMK固件
一.DMK固件 从罗克韦尔自动化下载的固件通常是以DMK为后缀名的文件: DMK文件无法直接使用,需要使用DMK Extraction Tool解压: 二.方法步骤 1. 如果从官网下载的固件文件后 ...
- C#解压文件,Excel操作
/// <summary> /// 获取目录下文件路径 /// </summary> /// <param name="path"></p ...
- shell脚本中关于getopts的使用方法
例子: while getopts ":e:s:pd:l" arg; do case $arg in e) ghs_env=$OPTARG ;; s) ghs_service=$O ...