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 ...
随机推荐
- SpringBoot-整合Swagger2
swagger2是一个用于生成.并能直接调用的可是话restful风格的服务 下面贴出springboot整合swagger2代码 一.maven依赖 这里使用的spring-boot版本是2.1.1 ...
- functools:管理函数的工具
介绍 functools模块提供了一些工具来管理或扩展和其他callable对象,从而不必完全重写 修饰符 偏函数partial from functools import partial ''' f ...
- 使用eclipse根据wsdl生成客户端
1.在需要生成的java项目右键new →other→ Web Service Client . 2.点击 Next.进入下面的界面,选择Brown...,选择WSDL,然后选择Next 3.Serv ...
- 超简单!教你如何修改源列表(sources.list)来提高软件访问速度
因为Ubuntu官方的源地址不在国内,所以在国内的访问速度非常慢,比如:我们要下载或是更新软件那速度比蜗牛还慢.所以,我们需要改成国内的镜像服务器,这样,我们在下载或更新软件的时候就会很快了. 配置步 ...
- API开发之接口安全(一)----生成sign
在对于API的开发中 最让人头疼的 就是接口数据暴露 让一些有心之人 抓包之后恶意请求 那么如何解决这一弊端呢?自然而然的 我们就想到了 加密 那我们又如何加密 如何解密 才能使之有最安全的效率呢? ...
- java-面试题为什么redis这么快
文章:为什么说Redis是单线程的以及Redis为什么这么快! 文章比较详细,有些细节可能需要注意. 1,比如CPU不是redis的瓶颈 2,随着连接数的增加,并发会降低等.
- zencart设置产品始终免运费sql
zencart网站后台-Tools(工具)-Install SQL Patches(安装SQL脚本): 运行以下相应sql语句,即可实现产品始终免运费. zencart设置所有产品始终免运费: '; ...
- XShell 假死
使用vim时因为使用windows word带来的坏习惯经常喜欢ctrl+s ,而这个造成的结果就是xshell假死,解决办法是ctrl+q
- C# 反射简单介绍
原文:https://blog.csdn.net/wu1020300665/article/details/82958455 1.什么是反射 反射是.NET中的重要机制,通过反射,可以在运行时获得程序 ...
- Android浮窗权限研究(转载)
这篇博客主要介绍的是 Android 主流各种机型和各种版本的悬浮窗权限适配,但是由于碎片化的问题,所以在适配方面也无法做到完全的主流机型适配,这个需要大家的一起努力,这个博客的名字永远都是一个将来时 ...