2023-12-09 18:42:21 +08:00
|
|
|
|
<p>给你一棵二叉树的根节点,返回该树的 <strong>直径</strong> 。</p>
|
2022-03-27 20:52:13 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p>二叉树的 <strong>直径</strong> 是指树中任意两个节点之间最长路径的 <strong>长度</strong> 。这条路径可能经过也可能不经过根节点 <code>root</code> 。</p>
|
|
|
|
|
|
|
|
|
|
<p>两节点之间路径的 <strong>长度</strong> 由它们之间边数表示。</p>
|
2022-03-27 20:52:13 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p> </p>
|
2022-03-27 20:52:13 +08:00
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong class="example">示例 1:</strong></p>
|
|
|
|
|
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg" style="width: 292px; height: 302px;" />
|
|
|
|
|
<pre>
|
|
|
|
|
<strong>输入:</strong>root = [1,2,3,4,5]
|
|
|
|
|
<strong>输出:</strong>3
|
|
|
|
|
<strong>解释:</strong>3 ,取路径 [4,2,1,3] 或 [5,2,1,3] 的长度。
|
2022-03-27 20:52:13 +08:00
|
|
|
|
</pre>
|
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong class="example">示例 2:</strong></p>
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
<strong>输入:</strong>root = [1,2]
|
|
|
|
|
<strong>输出:</strong>1
|
|
|
|
|
</pre>
|
2022-03-27 20:52:13 +08:00
|
|
|
|
|
|
|
|
|
<p> </p>
|
|
|
|
|
|
2023-12-09 18:42:21 +08:00
|
|
|
|
<p><strong>提示:</strong></p>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>树中节点数目在范围 <code>[1, 10<sup>4</sup>]</code> 内</li>
|
|
|
|
|
<li><code>-100 <= Node.val <= 100</code></li>
|
|
|
|
|
</ul>
|