|
| 1 | +<h2>108. Convert Sorted Array to Binary Search Tree</h2><h3>Easy</h3><hr><div><p>Given an integer array <code>nums</code> where the elements are sorted in <strong>ascending order</strong>, convert <em>it to a <strong>height-balanced</strong> binary search tree</em>.</p> |
| 2 | + |
| 3 | +<p>A <strong>height-balanced</strong> binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong>Example 1:</strong></p> |
| 7 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;"> |
| 8 | +<pre><strong>Input:</strong> nums = [-10,-3,0,5,9] |
| 9 | +<strong>Output:</strong> [0,-3,9,-10,null,5] |
| 10 | +<strong>Explanation:</strong> [0,-10,5,null,-3,null,9] is also accepted: |
| 11 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;"> |
| 12 | +</pre> |
| 13 | + |
| 14 | +<p><strong>Example 2:</strong></p> |
| 15 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;"> |
| 16 | +<pre><strong>Input:</strong> nums = [1,3] |
| 17 | +<strong>Output:</strong> [3,1] |
| 18 | +<strong>Explanation:</strong> [1,3] and [3,1] are both a height-balanced BSTs. |
| 19 | +</pre> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong>Constraints:</strong></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li><code>1 <= nums.length <= 10<sup>4</sup></code></li> |
| 26 | + <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> |
| 27 | + <li><code>nums</code> is sorted in a <strong>strictly increasing</strong> order.</li> |
| 28 | +</ul> |
| 29 | +</div> |
0 commit comments