1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/二叉树的直径 [diameter-of-binary-tree].html

32 lines
1.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一棵二叉树的根节点,返回该树的 <strong>直径</strong></p>
<p>二叉树的 <strong>直径</strong> 是指树中任意两个节点之间最长路径的 <strong>长度</strong> 。这条路径可能经过也可能不经过根节点 <code>root</code></p>
<p>两节点之间路径的 <strong>长度</strong> 由它们之间边数表示。</p>
<p>&nbsp;</p>
<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] 的长度。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>root = [1,2]
<strong>输出:</strong>1
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数目在范围 <code>[1, 10<sup>4</sup>]</code></li>
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
</ul>