Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 22c68f4

Browse files
add LeetCode 108. 将有序数组转换为二叉搜索树
1 parent 49bb01d commit 22c68f4

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
6+
7+
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
8+
9+
示例:
10+
11+
```javascript
12+
给定有序数组: [-10,-3,0,5,9],
13+
14+
一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:
15+
16+
0
17+
/ \
18+
-3 9
19+
/ /
20+
-10 5
21+
```
22+
23+
来源:力扣(LeetCode)
24+
链接:https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree
25+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
26+
27+
28+
29+
## 解题思路
30+
题目要求高度平衡——构建 root 时,选数组的中间元素作为 root 节点值,即可保证平衡。
31+
32+
类似分治算法一样,对中间位置确定根节点 root ,然后递归左子树和右子树,最终走完后就是我们的高度平衡的子树。
33+
![](https://img-blog.csdnimg.cn/20200924192306117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
34+
35+
参考 <a href="https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/solution/shou-hua-tu-jie-di-gui-fen-zhi-by-hyj8/">笨猪爆破组</a> 大佬题解
36+
37+
```javascript
38+
/**
39+
* Definition for a binary tree node.
40+
* function TreeNode(val) {
41+
* this.val = val;
42+
* this.left = this.right = null;
43+
* }
44+
*/
45+
/**
46+
* @param {number[]} nums
47+
* @return {TreeNode}
48+
*/
49+
var sortedArrayToBST = function (nums) {
50+
let buildBST = (nums, start, end) => {
51+
if (start > end) return null;
52+
// 找到中间位置
53+
let mid = (start + end) >>> 1;
54+
// 创建根节点
55+
let root = new TreeNode(nums[mid]);
56+
// 构建左子树
57+
root.left = buildBST(nums, start, mid - 1);
58+
// 构建右子树
59+
root.right = buildBST(nums, mid + 1, end);
60+
return root;
61+
}
62+
return buildBST(nums,0,nums.length-1);
63+
};
64+
```
65+
66+
67+
## 最后
68+
文章产出不易,还望各位小伙伴们支持一波!
69+
70+
往期精选:
71+
72+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
73+
74+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
75+
76+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
77+
78+
79+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
80+
81+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
82+
83+
```javascript
84+
学如逆水行舟,不进则退
85+
```
86+
87+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /