mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			32 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			32 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一棵二叉树的根节点,返回该树的 <strong>直径</strong> 。</p>
 | 
						||
 | 
						||
<p>二叉树的 <strong>直径</strong> 是指树中任意两个节点之间最长路径的 <strong>长度</strong> 。这条路径可能经过也可能不经过根节点 <code>root</code> 。</p>
 | 
						||
 | 
						||
<p>两节点之间路径的 <strong>长度</strong> 由它们之间边数表示。</p>
 | 
						||
 | 
						||
<p> </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> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li>树中节点数目在范围 <code>[1, 10<sup>4</sup>]</code> 内</li>
 | 
						||
	<li><code>-100 <= Node.val <= 100</code></li>
 | 
						||
</ul>
 |