Submission Detail

31 / 31 test cases passed.
Status:

Accepted

Runtime: 0 ms
Memory Usage: 43.3 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
80
85
90
95
100
105
110
java
Runtime (ms)
Distribution (%)

0
1
2
3
4
5
6
7
8
9
10
80
90
100
110
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

41000
41250
41500
41750
42000
42250
42500
42750
43000
43250
43500
43750
2
4
6
8
10
java
You are here!
Your memory usage beats 51.08 % of java submissions.
Memory (KB)
Distribution (%)

41000
41500
42000
42500
43000
43500
5
10
Zoom area by dragging across this chart

Invite friends to challenge Convert Sorted Array to Binary Search Tree


Submitted Code: 0 minutes ago

Language: java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
int mp = nums.length/2;
TreeNode root = new TreeNode(nums[mp]);
buildBST(root,mp,nums);
return root;
}
public void buildBST(TreeNode root, int mp, int[] nums) {
if (nums.length <= 1)
return;
int[] left = new int[mp];
int[] right = new int[nums.length-mp-1];
for (int i = 0; i < mp; i++)
left[i] = nums[i];
for (int i = mp+1; i < nums.length; i++)
right[i-mp-1] = nums[i];
int left_mp = left.length/2;
int right_mp = right.length/2;
TreeNode l = new TreeNode(left[left_mp]);
TreeNode r = new TreeNode();
if (right.length != 0)
r.val = right[right_mp];
root.left = l;
if (right.length != 0)
root.right = r;
buildBST(l,left_mp,left);
buildBST(r,right_mp,right);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX