1
- # 题目地址(109. 有序链表转换二叉搜索树)
1
+ ## 题目地址(109. 有序链表转换二叉搜索树)
2
2
3
3
https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
4
4
@@ -21,78 +21,85 @@ https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
21
21
/ /
22
22
-10 5
23
23
```
24
+
24
25
## 前置知识
25
26
- 递归
26
27
- 二叉搜索树
27
- > 对于树中任意一个点,当前节点的值必然大于所有左子树节点的值
28
+ > 对于树中任意一个点,当前节点的值必然大于所有左子树节点的值
28
29
同理,当前节点的值必然小于所有右子树节点的值
29
30
30
-
31
31
## 思路
32
32
1 . 获取当前链表的中点
33
33
2 . 以链表中点为根
34
34
3 . 中点左边的值都小于它,可以构造左子树,
35
35
4 . 同理构造右子树
36
36
5 . 循环第一步
37
37
38
-
39
38
### 双指针法
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
+ ** 复杂度分析**
87
91
- 时间复杂度:节点最多只遍历N* logN遍,时间复杂度为$O(NlogN)$
88
92
- 空间复杂度:空间复杂度为$O(1)$
89
93
90
- ### 缓存法
94
+ ### 缓存法
91
95
因为链表访问中点的时间复杂度为O(n),所以可以使用数组将链表的值存储,以空间换时间
92
96
93
- ** 代码**
97
+ ### 代码
98
+
99
+ - 代码支持: JS, Go, PHP
94
100
95
101
JS Code:
102
+
96
103
``` js
97
104
var sortedListToBST = function (head ) {
98
105
let res = []
@@ -114,6 +121,7 @@ function run(res){
114
121
```
115
122
116
123
Go Code:
124
+
117
125
``` go
118
126
/* *
119
127
* Definition for singly-linked list.
@@ -152,6 +160,7 @@ func BST109(a []int) *TreeNode {
152
160
```
153
161
154
162
PHP Code:
163
+
155
164
``` php
156
165
/**
157
166
* Definition for a singly-linked list.
0 commit comments