leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)
题目
https://leetcode.com/problems/maximum-width-of-binary-tree/
题解
本题思路来源于二叉树的层序遍历。
- 层序遍历类似问题:leetcode 199. Binary Tree Right Side View | 199. 二叉树的右视图(Java)
- 如何使用“记录下一行最右节点”的方式,进行层序遍历:左神算法:二叉树的按层打印与ZigZag打印
一开始就想到了给每一个元素维护 position,但误以为 左孩子 pos-1,右孩子 pos+1,提交之后才发现并不是这样的。看了下答案的提示:
答案
这个问题中的主要想法是,利用完全二叉树(用数组存储堆)的性质,给每个节点一个 position
值,如果我们走向左子树,那么 position -> position * 2
,如果我们走向右子树,那么 position -> positon * 2 + 1
。当我们在看同一层深度的位置值 L
和 R
的时候,宽度就是 R - L + 1
。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Info {TreeNode node;int index;public Info(TreeNode node, int index) {this.node = node;this.index = index;}
}class Solution {public int widthOfBinaryTree(TreeNode root) {Queue<Info> queue = new LinkedList<>();Info nextLast = new Info(root, 0);Info curFirst = nextLast;Info curLast = nextLast;queue.add(nextLast);int result = 0;while (queue.size() > 0) {Info poll = queue.poll();if (poll.node.left != null) {Info left = new Info(poll.node.left, poll.index * 2);queue.offer(left);nextLast = left;}if (poll.node.right != null) {Info right = new Info(poll.node.right, poll.index * 2 + 1);queue.offer(right);nextLast = right;}if (poll.node == curLast.node) { // after this node, goto a new lineresult = Math.max(result, curLast.index - curFirst.index + 1);curFirst = queue.peek();curLast = nextLast;}}return result;}
}
leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)相关推荐
- LeetCode 662. Maximum Width of Binary Tree
原题链接在这里:https://leetcode.com/problems/maximum-width-of-binary-tree/ 题目: Given a binary tree, write a ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode: 104. Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- leetcode 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 ...
- LeetCode之Maximum Depth of Binary Tree
1.题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- [swift] LeetCode 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 ...
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
最新文章
- memcached图形界面的监控
- 腾讯云 cloudbase 云开发使用笔记
- BZOJ3597 [Scoi2014]方伯伯运椰子 【二分 + 判负环】
- 你经历过最奇特的梦境是怎样的?
- HDU 6602 Longest Subarray (线段树)
- java.lang.ClassFormatError
- centos7的firewall-cmd怎么让指定ip能访问指定端口?
- 融云 SDK 5.0.0 功能迭代
- react app 拉起微信、支付宝支付
- win7安装程序无法配置计算机,windows安装程序无法将windows配置在此计算机的硬件上运行的解决方法...
- JPA之Specification复杂条件查询
- Excel文档瘦身,一键压缩xlsx文件中所有的图片,解决excel文件太大的问题
- 终端操作GitHub代码以及代码的版本控制(develop/master)多图
- ssh配置和多平台ssh配置
- 3-33在图 3-31中,以太网交换机有6个接口,分别接到5台主机和一个路由器。在下面表中的“动作”一栏中,表示先后发送了4个帧。假定在开始时,以太网交换 机的交换表是空的。试把该表中其他的栏目都填写
- 通过人工智能实现内容智能审核及在世界杯的实战
- python的out模式_Python设计模式之状态模式
- Android延时执行事件的方法
- 全栈AI火力全开,“云智一体”为开发者凿开产业智能通衢
- Vue中qs插件的使用
热门文章
- CodeForces - 722C Destroying Array(倒着并查集+离线处理)
- UVA - 10480 Sabotage(最小割-最大流+输出割边)
- POJ - 3926 Parade(单调队列优化dp)
- 牛客 - 排序(模拟)
- 证件照排版软件_证件照小程序换背景(制作免费版)
- UVA211 TheDomino Effect 多米诺效应
- 安卓入门系列-09一个实战小项目(备忘录Memo)
- python3爬虫(6)爬虫代理的使用
- Delphi使用ADO组件访问ACCESS数据入门例程
- 纯虚函数能为private吗?