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的更多相关文章

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

  2. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

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

  4. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

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

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

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

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

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

随机推荐

  1. QQ 客服设置

    不说那么多了. 目前可以通过此方式实现添加的效果 <a target="_blank" href="http://wpa.qq.com/msgrd?v=3& ...

  2. Python: 读文件,写文件

    读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...

  3. Jackson基础

    一.所需jar包: jackson-core-x.x.x-rc4.jar.jackson-databind-x.x.x-rc4.jar.jackson-annotations-x.x.x-rc4.ja ...

  4. linux 安装软件的几种方法

    一. 解析Linux应用软件安装包: 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rp ...

  5. 数据仓库基础(三)OLAP

    本文转载自:http://www.cnblogs.com/evencao/archive/2013/06/14/3135589.html 联机处理分析(OLAP):介绍 首先要理解的概念: 1.维度: ...

  6. Java(20~24)

    1.Collection中的集合称为单列集合,Map中的集合称为双列集合(键值对集合). 2.Map常用方法:map.put()   map.get()   map.remove()   map.ke ...

  7. Linux服务器配置---phpmyadmin

    phpMyAdmin 工具 1.检测是否已安装php.php-mysql.apache等工具 [root@localhost src]# rpm -qa |grep php php-cli-5.3.3 ...

  8. 20145316许心远《网络对抗》EXP7网络欺诈技术防范

    20145316许心远<网络对抗>EXP7网络欺诈技术防范 实验后回答问题 通常在什么场景下容易受到DNS spoof攻击 公共共享网络里,同一网段可以ping通的网络非常容易被攻击 在日 ...

  9. python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法

    python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...

  10. 小白也能看懂的插件化DroidPlugin原理(一)-- 动态代理

    前言:插件化在Android开发中的优点不言而喻,也有很多文章介绍插件化的优势,所以在此不再赘述.前一阵子在项目中用到 DroidPlugin 插件框架 ,近期准备投入生产环境时出现了一些小问题,所以 ...