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 ...
随机推荐
- 【Day1】3.数据类型
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- SQL语句复习【专题七】
SQL语句复习[专题七] 完整性约束分类1)域完整性约束(非空not null,检查check)2)实体完整性约束(唯一unique,主键primary key)3)参照完整性约束(外键foreign ...
- Linux基础知识之文件的权限(二)
除了基本的r,w,x之外,在linux传统的ext2.ext3.ext4文件系统下,还可以设置其他 的文件属性.如chattr,lsattr,而在CentOS7中默认利用xfs作为默认的文件系统,就不 ...
- JavaScript在页面中的执行顺序(理解声明式函数与赋值式函数) 转载
JavaScript在页面中的执行顺序 https://blog.csdn.net/superhoy/article/details/52946277 2016年10月27日 15:38:52 阅读数 ...
- phpStorm中使用xdebug工具调试docker容器中的程序
前提准备 phpstorm开发软件 + dnmp(docker + nginx + mysql +php) 配置好hosts 映射比如 /etc/hosts 127.0.0.1 tp5.de ...
- kotlin面向对象入门
之前在学kotlin基础语法时咱们是采用三方jar包在eclipse工程下进行的,很显然这工具在实际商用中基本上很少用到了,最终是要编写android程序,所以说从这里起得更换一个更加智能更加贴近实际 ...
- qt libusb
https://github.com/Kakadu/Qt_libusb https://github.com/mcvsama/libusbcc/tree/master/src/libusbcc htt ...
- Java通过腾讯邮箱发送邮件
private static void sendEmaill() { Properties prop = new Properties(); //协议 prop.setProperty("m ...
- Blend Visual studio 和Visual studio 的区别
blend for visual studio 这个并不是用于开发中写代码的,而是专门用来做WPF的界面设计的.这是为了让专业的人做专业的事,尽量让写代码的人只使用visual studio,而做界面 ...
- mysqldump恢复
mysqldump的恢复操作比较简单,因为备份的文件就是导出的SQL语句,一般只需要执行这个文件就可以了,可以通过以下的方法. 方法一 [root@zstedu andyxi3306]# mysql ...