Skip to content

Commit 3223ec7

Browse files
committed
add 109
1 parent c9ae620 commit 3223ec7

File tree

7 files changed

+421
-1
lines changed

7 files changed

+421
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Idx | Date | Question | Python| Java | Domain | Tag | Difficulty | Remark
4545
039|20200425|[705. Design HashSet](https://leetcode.com/problems/design-hashset/)|[705p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/705_Design_HashSet.ipynb)|[705j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/705/Solution.java)|Hash| |Easy
4646
040|20200425|[13. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)|[013p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/013_Roman_to_Integer.ipynb)|[013j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/013/Solution.java)|Math| |Easy
4747
041|20200425|[12. Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[012p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/012_Integer_to_Roman.ipynb)|[012j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/012/Solution.java)|Math| |Medium
48-
042|20200627|[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[108p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/108_Convert_Sorted_Array_to_Binary_Search_Tree.ipynb)|[108j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/108/Solution.java)|BST|Recursive|Easy
48+
042|20200627|[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[108p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/108_Convert_Sorted_Array_to_Binary_Search_Tree.ipynb)|[108j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/108/Solution.java)|BST|Recursive|Easy
49+
043|20200627|[109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)|[109p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/109_Convert_Sorted_List_to_Binary_Search_Tree.ipynb)|[109j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/109/Solution.java)|BST|Recursive|Middle

images/109_Q.PNG

55.1 KB
Loading

images/109_S1.PNG

25.4 KB
Loading

images/109_S2.PNG

25.2 KB
Loading

ipynb_files/109_Convert_Sorted_List_to_Binary_Search_Tree.ipynb

Lines changed: 330 additions & 0 deletions
Large diffs are not rendered by default.

java_codes/109/Solution.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.List;
2+
3+
//Definition for singly-linked list.
4+
class ListNode {
5+
int val;
6+
ListNode next;
7+
ListNode() {}
8+
ListNode(int val) { this.val = val; }
9+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
10+
}
11+
12+
class TreeNode {
13+
int val;
14+
TreeNode left;
15+
TreeNode right;
16+
TreeNode() {}
17+
TreeNode(int val) { this.val = val; }
18+
TreeNode(int val, TreeNode left, TreeNode right) {
19+
this.val = val;
20+
this.left = left;
21+
this.right = right;
22+
}
23+
}
24+
25+
// method 1: recursion
26+
class Solution {
27+
public TreeNode sortedListToBST(ListNode head) {
28+
if (head == null){
29+
return null;
30+
}
31+
ListNode mid = findMiddle(head);
32+
TreeNode node = new TreeNode(mid.val);
33+
if (mid == head){
34+
return node;
35+
}
36+
node.left = sortedListToBST(head);
37+
node.right = sortedListToBST(mid.next);
38+
return node;
39+
}
40+
41+
private ListNode findMiddle(ListNode head){
42+
ListNode prevPtr = null, slowPtr = head, fastPtr = head;
43+
while ((fastPtr != null ) && (fastPtr.next != null)){
44+
prevPtr = slowPtr;
45+
slowPtr = slowPtr.next;
46+
fastPtr = fastPtr.next.next;
47+
}
48+
if (prevPtr != null){
49+
prevPtr.next = null;
50+
}
51+
return slowPtr;
52+
}
53+
}
54+
55+
// method 2: inorder simulation
56+
class Solution {
57+
58+
private ListNode head;
59+
60+
private int findSize(ListNode head){
61+
ListNode ptr = head;
62+
int c = 0;
63+
while (ptr != null){
64+
ptr = ptr.next;
65+
c += 1;
66+
}
67+
return c;
68+
}
69+
70+
private TreeNode convertListToBST(int l, int r){
71+
if (l > r){
72+
return null;
73+
}
74+
int mid = (l + r) / 2;
75+
TreeNode left = this.convertListToBST(l, mid - 1);
76+
TreeNode node = new TreeNode(this.head.val);
77+
node.left = left;
78+
this.head = this.head.next;
79+
node.right = this.convertListToBST(mid + 1, r);
80+
return node;
81+
}
82+
83+
public TreeNode sortedListToBST(ListNode head) {
84+
int size = findSize(head);
85+
this.head = head;
86+
return convertListToBST(0, size - 1);
87+
}
88+
}

java_codes/java_codes.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<sourceFolder url="file://$MODULE_DIR$/013" isTestSource="false" />
4747
<sourceFolder url="file://$MODULE_DIR$/012" isTestSource="false" />
4848
<sourceFolder url="file://$MODULE_DIR$/108" isTestSource="false" />
49+
<sourceFolder url="file://$MODULE_DIR$/109" isTestSource="false" />
4950
</content>
5051
<orderEntry type="inheritedJdk" />
5152
<orderEntry type="sourceFolder" forTests="false" />

0 commit comments

Comments
 (0)