1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/将二叉搜索树变平衡 [balance-a-binary-search-tree].html

34 lines
1.3 KiB
HTML
Raw Normal View History

2022-03-27 20:37:52 +08:00
<p>给你一棵二叉搜索树,请你返回一棵&nbsp;<strong>平衡后</strong>&nbsp;的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。如果有多种构造方法,请你返回任意一种。</p>
<p>如果一棵二叉搜索树中,每个节点的两棵子树高度差不超过 <code>1</code> ,我们就称这棵二叉搜索树是&nbsp;<strong>平衡的</strong></p>
<p>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树节点的数目在&nbsp;<code>[1, 10<sup>4</sup>]</code>&nbsp;范围内。</li>
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>