Skip to content

Commit 244121a

Browse files
committed
sovle problem Convert Sorted List To Binary Search Tree
1 parent 0acde1d commit 244121a

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All solutions will be accepted!
2020
|226|[Invert Binary Tree](https://leetcode-cn.com/problems/invert-binary-tree/description/)|[java/py/js](./algorithms/InvertBinaryTree)|Easy|
2121
|693|[Binary Number With Alternating Bits](https://leetcode-cn.com/problems/binary-number-with-alternating-bits/description/)|[java/py/js](./algorithms/BinaryNumberWithAlternatingBits)|Easy|
2222
|108|[Convert Sorted Array To Binary Search Tree](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)|[java/py/js](./algorithms/ConvertSortedArrayToBinarySearchTree)|Easy|
23+
|109|[Convert Sorted List To Binary Search Tree](https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/description/)|[java/py/js](./algorithms/ConvertSortedListToBinarySearchTree)|Medium|
2324
|561|[Array Partition I](https://leetcode-cn.com/problems/array-partition-i/description/)|[java/py/js](./algorithms/ArrayPartitionI)|Easy|
2425
|463|[Island Perimeter](https://leetcode-cn.com/problems/island-perimeter/description/)|[java/py/js](./algorithms/IslandPerimeter)|Easy|
2526
|821|[Shortest Distance To A Character](https://leetcode-cn.com/problems/shortest-distance-to-a-character/description/)|[java/py/js](./algorithms/ShortestDistanceToACharacter)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Convert Sorted List To Binary Search Tree
2+
We can solve this problem by recursive, like [Convert Sorted Array To Binary Search Tree](./ConvertSortedArrayToBinarySearchTree)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public TreeNode sortedListToBST(ListNode head) {
20+
int length = 0;
21+
ListNode p = head;
22+
23+
while (p != null) {
24+
p = p.next;
25+
length++;
26+
}
27+
return help(head, length);
28+
}
29+
30+
public TreeNode help(ListNode head, int length) {
31+
if (length == 0) return null;
32+
33+
ListNode p = head;
34+
int mid = length / 2;
35+
36+
for (int i = 0; i < mid; i++) p = p.next;
37+
38+
TreeNode root = new TreeNode(p.val);
39+
root.left = help(head, mid);
40+
root.right = help(p.next, length - mid - 1);
41+
return root;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {ListNode} head
17+
* @return {TreeNode}
18+
*/
19+
var sortedListToBST = function(head) {
20+
let length = 0,
21+
p = head
22+
23+
while (p) {
24+
p = p.next
25+
length++
26+
}
27+
28+
return help(head, length)
29+
};
30+
31+
var help = function (head, length) {
32+
if (length == 0) return null
33+
34+
let p = head,
35+
mid = parseInt(length / 2)
36+
37+
for (let i = 0; i < mid; i++) p = p.next
38+
39+
let root = new TreeNode(p.val)
40+
root.left = help(head, mid)
41+
root.right = help(p.next, length - mid - 1)
42+
return root
43+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
# Definition for a binary tree node.
8+
# class TreeNode(object):
9+
# def __init__(self, x):
10+
# self.val = x
11+
# self.left = None
12+
# self.right = None
13+
14+
class Solution(object):
15+
def sortedListToBST(self, head):
16+
"""
17+
:type head: ListNode
18+
:rtype: TreeNode
19+
"""
20+
length = 0
21+
p = head
22+
while p:
23+
p = p.next
24+
length += 1
25+
26+
return self.help(head, length)
27+
28+
def help(self, head, length):
29+
if length == 0: return None
30+
31+
p = head
32+
mid = length / 2
33+
34+
for i in xrange(mid):
35+
p = p.next
36+
37+
root = TreeNode(p.val)
38+
root.left = self.help(head, mid)
39+
root.right = self.help(p.next, length - mid - 1)
40+
return root

0 commit comments

Comments
 (0)