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 ...
随机推荐
- deep_learning_neural network梯度下降
神经网络优化算法:梯度下降法.Momentum.RMSprop和Adam 最近回顾神经网络的知识,简单做一些整理,归档一下神经网络优化算法的知识.关于神经网络的优化,吴恩达的深度学习课程讲解得非常通俗 ...
- ORALCE 数据库字符串处理、常用函数
.字符串转日期: to_date(paramStr,'YYYYMMDDHH24MISS') to_date(paramStr,'yyyy-MM-DD') to_date(paramStr,'yyyy/ ...
- 微信小程序开发(二)创建一个小程序页面
为了方便讲解,我们将上篇博客创建的小程序除了project.config.json和sitemap.json两个文件保留,其他全部删除(这两个文件存的是小程序的创建信息,删掉会有报错提示). 接下来我 ...
- 6.高性能NIO框架netty
1.Netty简介: Netty是基于Java NIO的网络应用框架 Netty是一个NIO client-server(客户端服务器)框架,使用Netty可以快速开发网络应用,例如服务器和客户端协议 ...
- 1.python环境安装
一:安装Python与环境配置 二:安装pip 三:Anaconda安装和使用 3.1 什么是 Anaconda? Anaconda是专注于数据分析的Python发行版本,支持 Linux, Mac, ...
- java 多线程,线程安全等定义
线程安全, synchronized的使用,保证方法或代码块操作的原子性.可见性和有序性 参考这篇文章: 7张图带你轻松理解Java 线程安全 public class ThreadDemo { pr ...
- Redis 与 MQ 的区别
Redis是一个高性能的key-value数据库,它的出现很大程度补偿了memcached这类key-value存储的不足.虽然它是一个数据库系统,但本身支持MQ功能,完全可以当做一个轻量级的队列服务 ...
- linux 下 svn 重新定位 url
linux下重新定位SVN URL方法: 如果更换了SVN服务器,就需要重新定位,指向新的SVN URL. 重新定位命令:svn switch --relocate 原svn地址 新svn地址 ...
- mybatis使用@Insert @SelectKey 执行插入语句时获得主键自增长值(转)
@Insert(" insert into table(c1,c2) " + " values (#{c1},#{c2}) ") @SelectKey(resu ...
- Java的日期与时间 java.time.Duration (转)
一个Duration对象表示两个Instant间的一段时间,是在Java 8中加入的新功能. 一个Duration实例是不可变的,当创建出对象后就不能改变它的值了.你只能通过Duration的计算方法 ...