题目

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。当我们在看同一层深度的位置值 LR 的时候,宽度就是 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)相关推荐

  1. LeetCode 662. Maximum Width of Binary Tree

    原题链接在这里:https://leetcode.com/problems/maximum-width-of-binary-tree/ 题目: Given a binary tree, write a ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

最新文章

  1. memcached图形界面的监控
  2. 腾讯云 cloudbase 云开发使用笔记
  3. BZOJ3597 [Scoi2014]方伯伯运椰子 【二分 + 判负环】
  4. 你经历过最奇特的梦境是怎样的?
  5. HDU 6602 Longest Subarray (线段树)
  6. java.lang.ClassFormatError
  7. centos7的firewall-cmd怎么让指定ip能访问指定端口?
  8. 融云 SDK 5.0.0 功能迭代
  9. react app 拉起微信、支付宝支付
  10. win7安装程序无法配置计算机,windows安装程序无法将windows配置在此计算机的硬件上运行的解决方法...
  11. JPA之Specification复杂条件查询
  12. Excel文档瘦身,一键压缩xlsx文件中所有的图片,解决excel文件太大的问题
  13. 终端操作GitHub代码以及代码的版本控制(develop/master)多图
  14. ssh配置和多平台ssh配置
  15. 3-33在图 3-31中,以太网交换机有6个接口,分别接到5台主机和一个路由器。在下面表中的“动作”一栏中,表示先后发送了4个帧。假定在开始时,以太网交换 机的交换表是空的。试把该表中其他的栏目都填写
  16. 通过人工智能实现内容智能审核及在世界杯的实战
  17. python的out模式_Python设计模式之状态模式
  18. Android延时执行事件的方法
  19. 全栈AI火力全开,“云智一体”为开发者凿开产业智能通衢
  20. Vue中qs插件的使用

热门文章

  1. CodeForces - 722C Destroying Array(倒着并查集+离线处理)
  2. UVA - 10480 Sabotage(最小割-最大流+输出割边)
  3. POJ - 3926 Parade(单调队列优化dp)
  4. 牛客 - 排序(模拟)
  5. 证件照排版软件_证件照小程序换背景(制作免费版)
  6. UVA211 TheDomino Effect 多米诺效应
  7. 安卓入门系列-09一个实战小项目(备忘录Memo)
  8. python3爬虫(6)爬虫代理的使用
  9. Delphi使用ADO组件访问ACCESS数据入门例程
  10. 纯虚函数能为private吗?