mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
34 lines
1.3 KiB
HTML
34 lines
1.3 KiB
HTML
<p>给你一棵二叉搜索树,请你返回一棵 <strong>平衡后</strong> 的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。如果有多种构造方法,请你返回任意一种。</p>
|
||
|
||
<p>如果一棵二叉搜索树中,每个节点的两棵子树高度差不超过 <code>1</code> ,我们就称这棵二叉搜索树是 <strong>平衡的</strong> 。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<p><img src="https://assets.leetcode.com/uploads/2021/08/10/balance1-tree.jpg" style="height: 319px; width: 500px;" /></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>root = [1,null,2,null,3,null,4,null,null]
|
||
<strong>输出:</strong>[2,1,3,null,null,null,4]
|
||
<strong>解释:</strong>这不是唯一的正确答案,[3,1,4,null,2,null,null] 也是一个可行的构造方案。
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<p><img src="https://assets.leetcode.com/uploads/2021/08/10/balanced2-tree.jpg" style="height: 145px; width: 224px;" /></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong> root = [2,1,3]
|
||
<strong>输出:</strong> [2,1,3]
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li>树节点的数目在 <code>[1, 10<sup>4</sup>]</code> 范围内。</li>
|
||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||
</ul>
|