Skip to content

Commit c9ae620

Browse files
committed
add 108
1 parent cea8293 commit c9ae620

File tree

7 files changed

+378
-0
lines changed

7 files changed

+378
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ 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

images/108_Q.PNG

54.3 KB
Loading

images/108_S1.PNG

25.5 KB
Loading

images/108_S2.PNG

25.6 KB
Loading

ipynb_files/108_Convert_Sorted_Array_to_Binary_Search_Tree.ipynb

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

java_codes/108/Solution.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Definition for a binary tree node.
2+
class TreeNode {
3+
int val;
4+
TreeNode left;
5+
TreeNode right;
6+
TreeNode() {}
7+
TreeNode(int val) { this.val = val; }
8+
TreeNode(int val, TreeNode left, TreeNode right) {
9+
this.val = val;
10+
this.left = left;
11+
this.right = right;
12+
}
13+
}
14+
15+
class Solution {
16+
public TreeNode sortedArrayToBST(int[] nums) {
17+
return convert(nums, 0, nums.length - 1);
18+
}
19+
20+
private TreeNode convert(int[] nums, int left, int right){
21+
if (left > right){
22+
return null;
23+
}
24+
int mid = (left + right) / 2;
25+
TreeNode node = new TreeNode(nums[mid]);
26+
node.left = convert(nums, left, mid - 1);
27+
node.right = convert(nums, mid + 1, right);
28+
return node;
29+
}
30+
}
31+
32+
33+
34+

java_codes/java_codes.iml

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

0 commit comments

Comments
 (0)