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 tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.
The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
|
|
|
|
|
|
|
|
题目大意
给定一个二叉树,求每层最左节点到最右节点距离的最大值。
解题思路
层序遍历,辅助队列,左节点的个数则为:i 2,右节点的个数则为:i 2 + 1
队列的首元素和末元素的差值 + 1即为宽度。
刚开始用的Int,但是会越界,于是改用Double.
代码
|
|