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 ...
随机推荐
- SVG矢量图学习实例
从W3school上学习了一下SVG矢量图形,感觉和HTML相比还是有一些新的元素和属性的,一时间不能全部记住,特此留下笔记,供遗忘时作为参考 <!DOCTYPE html> <!- ...
- vscode笔记(一)- vscode自动生成文件头部注释和函数注释
VsCode 自动生成文件头部注释和函数注释 作者:狐狸家的鱼 本文链接:vscode自动生成文件头部注释和函数注释 GitHub:sueRimn 1.安装插件KoroFileHeader 2.设置 ...
- 结构体addrinfo, sockaddr, sockaddr_in的区别
struct sockaddr和struct sockaddr_in这两个结构体用来处理网络通信的地址. 一.sockaddr sockaddr在头文件#include <sys/socket. ...
- CentOS7_防火墙
1.查看防火墙状态 firewall-cmd --state 2.启动防火墙 systemctl start firewalld.service 3.关闭防火墙 systemctl stop fire ...
- design language
design language https://en.wikipedia.org/wiki/Design_language 设计语言(设计词汇)是一种超架构的方案和风格, 它用于指导产品组件或者架构配 ...
- Linux忘记root密码 单用户模式 及启动加密
单用户模式: 在系统启动引导读秒时,按任意键进入系统选项 再按 e 键 选择第二项 按 e 进入编辑 输入 空格 1 然后回车 再按B 键 不需要密码即可进入系统 再passwd ...
- IDEA中debug启动tomcat报错。Error running t8:Unable to open debugger port(127.0.0.1:49225):java.net.BindException"Address alread in use:JVM_Bind"
解决办法: 1,如下图打开项目配置的tomcat的“Edit Configurations...” 2,打开“Startup/Connection”--------"Debug"- ...
- ef mysql
App.config <configuration> <configSections> <!-- For more information on Entity Frame ...
- 传输层--TCP和UDP的区别
UDP(用户数据报协议):为调用它的应用程序提供了一种不可靠.无连接的服务. TCP(传输控制协议):为调用它的应用程序提供了一种可靠的.面向连接的服务. 当设计一个网络应用程序时,该应用程序的开发人 ...
- CentOS7中启动Chrome报错ERROR:zygote_host_impl_linux.cc
网上查的解决了问题 1. 需要安装libexif 说明我已经安装了libexif 2. 从安全角度考虑,Chrome不应在root环境下运行,如果某些原因必须以root身份运行Chrome,可以运行下 ...