二叉树最大宽度 Maximum Width of Binary Tree
2018-07-27 15:55:13
问题描述:



问题求解:
题目中说明了最后的宽度计算其实是按照满二叉树来进行计算的,也就是说如果我们能够得到每层最左边的节点编号和最右边的节点编号,那么本题就可以进行解决了。
另外,在如何编号的问题上,既然是满二叉树,那么编号的方式自然是父节点i,左子节点2 * i,右子节点2 * i + 1。
    public int widthOfBinaryTree(TreeNode root) {
        return helper(root, 0, 1, new ArrayList<Integer>(), new ArrayList<Integer>());
    }
    private int helper(TreeNode root, int layer, int index, List<Integer> begin, List<Integer> end) {
        if (root == null) return 0;
        if (begin.size() == layer) {
            begin.add(index);
            end.add(index);
        }
        else end.set(layer, index);
        int cur = end.get(layer) - begin.get(layer) + 1;
        int l = helper(root.left, layer + 1, 2 * index, begin, end);
        int r = helper(root.right, layer + 1, 2 * index + 1, begin, end);
        return Math.max(cur, Math.max(l, r));
    }
二叉树最大宽度 Maximum Width of Binary Tree的更多相关文章
- [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 ...
 - 【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 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
		
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
 - [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 ...
 - [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 ...
 - [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree
		
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
 
随机推荐
- OAuth 白话简明教程 1.简述
			
转自:http://www.cftea.com/c/2016/11/6702.asp OAuth 白话简明教程 1.简述 OAuth 白话简明教程 2.授权码模式(Authorization Code ...
 - 修改SQL Server 的排序规则(转)
			
转自http://jimshu.blog.51cto.com/3171847/1095780/ 一.修改SQL Server服务器(实例)的排序规则 以下实验使用了SQL Server 2008 R2 ...
 - Bus,Exclusive access,memory attribute
			
指令LDREX,STREX是在armv6中新加的指令,配合AMBA3--AXI中的lock[1:0]信号. 在Atomic Access一节中是这么规定的:ARLOCK[1:0]/AWLOCK[1:0 ...
 - yii2redis安装
			
yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...
 - Applying the Kappa architecture in the telco industry
			
https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...
 - linux rsync同步工具
			
linux rsync同步工具 1.rsync介绍rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据同步备份的优秀工具.rsync软件适用于unix/linux/windows ...
 - where T : class含义
			
.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型where T : class ...
 - SNMP学习笔记之Centos7配置SNMP服务
			
0x00 安装yum源安装SNMP软件包 1.yum源安装SNMP服务: yum -y install net-snmp net-snmp-utils 2.查看SNMP版本号: snmpd -v 0x ...
 - STL与泛型编程(第一周)
			
part 1 C++模版简介 一,模版概观 1.模板 (Templates)是C++的一种特性,允许函数或类(对象)通过泛型(generic types)的形式表现或运行. 模板可以使得函数或类在对应 ...
 - jenkins 安装 + maven + git部署
			
1. 安装JDK 2. 安装maven 3. 安装git 4. 安装tomcat tar zxvf apache-tomcat-8.5.14.tar.gz 找到tomcat-->config-- ...