2023-12-09 18:42:21 +08:00
|
|
|
|
<p>给你一棵二叉树的根节点 <code>root</code> ,返回树的 <strong>最大宽度</strong> 。</p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p>树的 <strong>最大宽度</strong> 是所有层中最大的 <strong>宽度</strong> 。</p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<div class="original__bRMd">
|
|
|
|
|
<div>
|
|
|
|
|
<p>每一层的 <strong>宽度</strong> 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的 <code>null</code> 节点,这些 <code>null</code> 节点也计入长度。</p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p>题目数据保证答案将会在 <strong>32 位</strong> 带符号整数范围内。</p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p> </p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong>示例 1:</strong></p>
|
|
|
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg" style="width: 359px; height: 302px;" />
|
|
|
|
|
<pre>
|
|
|
|
|
<strong>输入:</strong>root = [1,3,2,5,3,null,9]
|
|
|
|
|
<strong>输出:</strong>4
|
|
|
|
|
<strong>解释:</strong>最大宽度出现在树的第 3 层,宽度为 4 (5,3,null,9) 。
|
2022-03-27 20:46:41 +08:00
|
|
|
|
</pre>
|
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong>示例 2:</strong></p>
|
|
|
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg" style="width: 442px; height: 422px;" />
|
2022-03-27 20:46:41 +08:00
|
|
|
|
<pre>
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<strong>输入:</strong>root = [1,3,2,5,null,null,9,6,null,7]
|
|
|
|
|
<strong>输出:</strong>7
|
|
|
|
|
<strong>解释:</strong>最大宽度出现在树的第 4 层,宽度为 7 (6,null,null,null,null,null,7) 。
|
2022-03-27 20:46:41 +08:00
|
|
|
|
</pre>
|
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong>示例 3:</strong></p>
|
|
|
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg" style="width: 289px; height: 299px;" />
|
2022-03-27 20:46:41 +08:00
|
|
|
|
<pre>
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<strong>输入:</strong>root = [1,3,2,5]
|
|
|
|
|
<strong>输出:</strong>2
|
|
|
|
|
<strong>解释:</strong>最大宽度出现在树的第 2 层,宽度为 2 (3,2) 。
|
2022-03-27 20:46:41 +08:00
|
|
|
|
</pre>
|
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p> </p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong>提示:</strong></p>
|
2022-03-27 20:46:41 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<ul>
|
|
|
|
|
<li>树中节点的数目范围是 <code>[1, 3000]</code></li>
|
|
|
|
|
<li><code>-100 <= Node.val <= 100</code></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|