LC 662. Maximum Width of Binary Tree
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
Example 1:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input:
1
/
3
/ \
5 3
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input:
1
/ \
3 2
/
5
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input:
1
/ \
3 2
/ \
5 9
/ \
6 7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
Runtime: 8 ms, faster than 28.24% of C++ online submissions for Maximum Width of Binary Tree.
看了一下更快的解法用的是递归,但是思路和我的是一样的。就是给每一个点都标上序号。
最快的解法竟然是用while嵌套BFS,看来递归花掉了很多时间,但我这个可能是由于有拷贝(nextq,q)。
/**
* 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 widthOfBinaryTree(TreeNode* root) {
queue<pair<TreeNode*, int>> q;
q.push({root, });
int ret = ;
while(!q.empty()){
int minval = INT_MAX, maxval = INT_MIN;
queue<pair<TreeNode*,int>> nextq;
while(!q.empty()){
auto p = q.front(); q.pop();
minval = min(minval, p.second); maxval = max(maxval, p.second);
if(p.first->left) nextq.push({p.first->left, *p.second});
if(p.first->right) nextq.push({p.first->right, *p.second+});
}
ret = max(ret, maxval - minval);
q = nextq;
}
return ret+;
}
};
LC 662. Maximum Width of Binary Tree的更多相关文章
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 662. Maximum Width of Binary Tree二叉树的最大宽度
[抄题]: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...
- 【leetcode】662. Maximum Width of Binary Tree
题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- 662. Maximum Width of Binary Tree
https://leetcode.com/problems/maximum-width-of-binary-tree/description/ /** * Definition for a binar ...
- [LeetCode]662. Maximum Width of Binary Tree判断树的宽度
public int widthOfBinaryTree(TreeNode root) { /* 层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1 而且要有两个变量,一个记录本层节点数, ...
- [LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...
- [Swift]LeetCode662. 二叉树最大宽度 | Maximum Width of Binary Tree
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...
- [LC] 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- php迭代器Iterator接口
以前也看过迭代器Iterator接口,感觉不如yied好用,因此实际工作中并没有用到过. 今天看了一篇网上的博客(https://www.cnblogs.com/wwjchina/p/7723499. ...
- Phoenix中的timestamp字段查询以及timestamp的比较查询
1 查询语法 SELECT TO_CHAR(createtime, 'yyyy-MM-dd HH:mm:ss') FROM USER 2 需要比较timestamp的语法查询 select * fro ...
- D-Link系列路由器漏洞挖掘入门
D-Link系列路由器漏洞挖掘入门 前言 前几天去上海参加了geekpwn,看着大神们一个个破解成功各种硬件,我只能在下面喊 6666,特别羡慕那些大神们.所以回来就决定好好研究一下路由器,争取跟上大 ...
- imx6 yocto移植 环境搭建
系统:ubuntu14.04 LTS 切换软件下载源,确保下载资源是最快. 安装必要软件工具: ~$ apt-get install vim ~$ apt-get install openssh-se ...
- 22_5mybatis多表操作
1.表之间的关系 一对多 多对一 一对一 多对多 举例: 用户和订单就是一对多 订单和用户就是多对一 一个用户可以下多个订单 多个订单属于同一个用户 人和身份证号就是一对一 一个人只能有一个身份证号 ...
- 查看ocx控件CLSID的方法(转载)
CLSID就是classID类的标识码 1.打开注册表,window + r ,输入regedit,确定 2.点击 编辑 选择查找 3.ok拉 参考:https://blog.csdn.net/u01 ...
- Cmd有关IP的部分命令
ping命令判断系统数据包在传送的时候至少会经过一个以上的路由器,当数据包经过一个路由器的时候,TTL就会自动减1,如果减到0了还是没有传送到目的主机,那么这个数据包就会自动丢失,这时路由器会发送一个 ...
- flutter 记录正则匹配
手机号正则匹配: // 正则匹配 static bool isChinaPhoneLegal(String str) { return new RegExp('^((13[0-9])|(15[^4]) ...
- Linux磁盘分区的实用管理命令
系统环境:Centos6.7 命令信息: 1.lsblk 列出分区信息,可以查看分区的光在目录和使用情况 (读取内存中的分区表信息) 2.fdisk 用来创建MBR分区(也可以创建GPT分区,但是 ...
- loj2718 「NOI2018」归程[Kruskal重构树+最短路]
关于Kruskal重构树可以翻阅本人的最小生成树笔记. 这题明显裸的Kruskal重构树. 然后这题限制$\le p$的边不能走,实际上就是要满足走最小边权最大的瓶颈路,于是跑最大生成树,构建Krus ...