Skip to content

Commit 77e8e1d

Browse files
[N-0] refactor 108
1 parent 6d9a36b commit 77e8e1d

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ Your ideas/fixes/algorithms are more than welcome!
546546
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_111.java)| O(n)|O(1)~O(h) | Easy| BFS, DFS
547547
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_110.java)| O(n)|O(1)~O(h) | Easy| DFS
548548
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_109.java)| O(n)|O(h) | Medium | DFS, Recursion
549-
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_108.java)| O(n)|O(h) | Medium| Tree
549+
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_108.java)| O(n)|O(h) | Easy | Tree
550550
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_107.java)| O(nlogn)|O(h) | Easy| BFS
551551
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_106.java)| O(n)|O(n) | Medium| Recursion, Tree
552552
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_105.java)| O(n)|O(n) | Medium| Recursion, Tree

src/main/java/com/fishercoder/solutions/_108.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
*/
1010
public class _108 {
1111

12-
public TreeNode sortedArrayToBST(int[] num) {
13-
return rec(num, 0, num.length - 1);
14-
}
12+
public static class Solution1 {
13+
public TreeNode sortedArrayToBST(int[] num) {
14+
return rec(num, 0, num.length - 1);
15+
}
1516

16-
public TreeNode rec(int[] num, int low, int high) {
17-
if (low > high) {
18-
return null;
17+
public TreeNode rec(int[] num, int low, int high) {
18+
if (low > high) {
19+
return null;
20+
}
21+
int mid = low + (high - low) / 2;
22+
TreeNode root = new TreeNode(num[mid]);
23+
root.left = rec(num, low, mid - 1);
24+
root.right = rec(num, mid + 1, high);
25+
return root;
1926
}
20-
int mid = low + (high - low) / 2;
21-
TreeNode root = new TreeNode(num[mid]);
22-
root.left = rec(num, low, mid - 1);
23-
root.right = rec(num, mid + 1, high);
24-
return root;
2527
}
2628

2729
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._108;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _108Test {
9+
private static _108.Solution1 solution1;
10+
private static int[] nums;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _108.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
nums = new int[]{1, 2, 3};
20+
TreeUtils.printBinaryTree(solution1.sortedArrayToBST(nums));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)