Skip to content

Commit c4cccea

Browse files
committed
Update 109.Convert-Sorted-List-to-Binary-Search-Tree.md
1 parent 0a98763 commit c4cccea

File tree

1 file changed

+62
-53
lines changed

1 file changed

+62
-53
lines changed

problems/109.Convert-Sorted-List-to-Binary-Search-Tree.md

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 题目地址(109. 有序链表转换二叉搜索树)
1+
## 题目地址(109. 有序链表转换二叉搜索树)
22

33
https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
44

@@ -21,78 +21,85 @@ https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
2121
/ /
2222
-10 5
2323
```
24+
2425
## 前置知识
2526
- 递归
2627
- 二叉搜索树
27-
> 对于树中任意一个点,当前节点的值必然大于所有左子树节点的值
28+
> 对于树中任意一个点,当前节点的值必然大于所有左子树节点的值
2829
同理,当前节点的值必然小于所有右子树节点的值
2930

30-
3131
## 思路
3232
1. 获取当前链表的中点
3333
2. 以链表中点为根
3434
3. 中点左边的值都小于它,可以构造左子树,
3535
4. 同理构造右子树
3636
5. 循环第一步
3737

38-
3938
### 双指针法
40-
1. 定义一个快指针每步前进两个节点,一个慢指针每步前进一个节点
41-
2. 当快指针到达尾部的时候,正好慢指针所到的点为中点
42-
js Code
43-
```js
44-
var sortedListToBST = function(head) {
45-
if(!head) return null;
46-
return run(head, null);
47-
};
48-
49-
function run(head, tail){
50-
if(head == tail) return null;
51-
let fast = head;
52-
let slow = head;
53-
while(fast != tail && fast.next != tail){
54-
fast = fast.next.next;
55-
slow = slow.next;
56-
}
57-
let root = new TreeNode(slow.val);
58-
root.left = run(head, slow);
59-
root.right = run(slow.next, tail);
60-
return root;
61-
}
62-
```
63-
64-
java Code:
65-
```java
66-
class Solution {
67-
public TreeNode sortedListToBST(ListNode head) {
68-
if(head == null) return null;
69-
return run(head,null);
70-
}
71-
private TreeNode run(ListNode head, ListNode tail){
72-
if(head == tail) return null;
73-
ListNode fast = head, slow = head;
74-
while(fast != tail && fast.next != tail){
75-
fast = fast.next.next;
76-
slow = slow.next;
77-
}
78-
TreeNode root = new TreeNode(slow.val);
79-
root.left = run(head, slow);
80-
root.right = run(slow.next, tail);
81-
return root;
82-
}
83-
}
84-
```
85-
86-
**复杂度分析**
39+
1. 定义一个快指针每步前进两个节点,一个慢指针每步前进一个节点
40+
2. 当快指针到达尾部的时候,正好慢指针所到的点为中点
41+
42+
- 语言支持: JS, Java
43+
44+
JS Code:
45+
46+
```js
47+
var sortedListToBST = function(head) {
48+
if(!head) return null;
49+
return run(head, null);
50+
};
51+
52+
function run(head, tail){
53+
if(head == tail) return null;
54+
let fast = head;
55+
let slow = head;
56+
while(fast != tail && fast.next != tail){
57+
fast = fast.next.next;
58+
slow = slow.next;
59+
}
60+
let root = new TreeNode(slow.val);
61+
root.left = run(head, slow);
62+
root.right = run(slow.next, tail);
63+
return root;
64+
}
65+
```
66+
67+
Java Code:
68+
69+
```java
70+
class Solution {
71+
public TreeNode sortedListToBST(ListNode head) {
72+
if(head == null) return null;
73+
return run(head,null);
74+
}
75+
private TreeNode run(ListNode head, ListNode tail){
76+
if(head == tail) return null;
77+
ListNode fast = head, slow = head;
78+
while(fast != tail && fast.next != tail){
79+
fast = fast.next.next;
80+
slow = slow.next;
81+
}
82+
TreeNode root = new TreeNode(slow.val);
83+
root.left = run(head, slow);
84+
root.right = run(slow.next, tail);
85+
return root;
86+
}
87+
}
88+
```
89+
90+
**复杂度分析**
8791
- 时间复杂度:节点最多只遍历N*logN遍,时间复杂度为$O(NlogN)$
8892
- 空间复杂度:空间复杂度为$O(1)$
8993

90-
### 缓存法
94+
### 缓存法
9195
因为链表访问中点的时间复杂度为O(n),所以可以使用数组将链表的值存储,以空间换时间
9296

93-
**代码**
97+
### 代码
98+
99+
- 代码支持: JS, Go, PHP
94100

95101
JS Code:
102+
96103
```js
97104
var sortedListToBST = function(head) {
98105
let res = []
@@ -114,6 +121,7 @@ function run(res){
114121
```
115122

116123
Go Code:
124+
117125
```go
118126
/**
119127
* Definition for singly-linked list.
@@ -152,6 +160,7 @@ func BST109(a []int) *TreeNode {
152160
```
153161

154162
PHP Code:
163+
155164
```php
156165
/**
157166
* Definition for a singly-linked list.

0 commit comments

Comments
 (0)