662. Maximum Width of Binary Tree
https://leetcode.com/problems/maximum-width-of-binary-tree/description/
/**
* 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) {
int left = , right = , res = ;
queue<pair<int,TreeNode*>> q;
if (root != NULL) q.push({, root});
while (!q.empty()) {
int qsz = q.size();
for (int i = ; i < qsz; i++) {
int idx = q.front().first;
TreeNode* cur = q.front().second;
q.pop(); if (i == ) left = idx;
if (i == qsz - ) right = idx;
if (cur->left) q.push( { idx *, cur->left });
if (cur->right) q.push( { idx * + , cur->right });
}
res = max(res, right-left+);
}
return res;
}
};
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 ...
- 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 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 a tre ...
- 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 ...
- [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 ...
- 二叉树最大宽度 Maximum Width of Binary Tree
2018-07-27 15:55:13 问题描述: 问题求解: 题目中说明了最后的宽度计算其实是按照满二叉树来进行计算的,也就是说如果我们能够得到每层最左边的节点编号和最右边的节点编号,那么本题就可以 ...
随机推荐
- JDK动态代理详解-依赖接口
0. 原理分析 a). 自定义实现InvocationHandler类,实现代理类执行时的invoke方法 b). 使用Proxy.newProxyInstance生成接口的代理类(入参还包括Invo ...
- Dedecms标签arclistsg调用单表模型出错的解决方法
使用arclistsg标签调用分类信息等单表模型出错提示Column 'id' in where clause is ambiguous, 修改文件:include\taglib\arclistsg ...
- 3年,阅读量100万+, Github Star 15000+
这两天突然发现,三年前在博客园写的一篇文章阅读量超过百万了,对,还是技术文章.这个让我蛮惊讶的,当时刚开始写这篇文章的时候,一周的阅读量也才两三千,随着时间慢慢的过去,在搜索引擎的加持下竟然超过了百万 ...
- Java图形界面开发—简易记事本
在学习了Java事件之后,自己写了一个极其简单的记事本.用到了MenuBar,Menu,MenuITem等控件,事件包括ActionListener以及KeyListener. 代码如下: ...
- 分布式缓存memcached介绍,win7环境安装,常用命令set,get,delete,stats, java访问
一.memcached是什么? 二.memcached不互相通信的分布式 三.安装步骤 四.本文介绍的命令主要包括: 存入命令(Storage commands) 取回命令(Retrieval com ...
- 制作centos安装u盘
格式化 mkfs.vfat /dev/sdb1 制作 dd if=CentOS-7-x86_64-Minimal-1503-01.iso of=/dev/sdb # 1. 注意是/dev/sdb 不是 ...
- PHP重定向的三个方法
js的重定向方法:location.href=目标 url(如 https:www.baidu.com); php的重定向方法: header("location: https:www.ba ...
- Jsoup进阶选择器
package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...
- windows环境下Nginx部署及Https设置
一.Nginx安装部署及常用命令. 1.1.其实Nginx是免安装的.直接在官网下载zip包,解压即可,下载地址:http://nginx.org/en/download.html,因为我这边的开发服 ...
- MYSQL短索引
优化MYSQL时,可以尽量使用短索引,如果只是为了提高读取的速度,可以优先使用聚合索引,把几个字段聚集在一起,当然缺点在于操作(写)的时候会降低效率,短索引一般都是开头几个字符基本不同的时候,可以考虑 ...