-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ108ConvertArrayToTree.java
More file actions
42 lines (37 loc) · 1.31 KB
/
Copy pathQ108ConvertArrayToTree.java
File metadata and controls
42 lines (37 loc) · 1.31 KB
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
/**
/*
@b-knd (jingru) on 08 August 2022 11:02:00
*/
/* 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) {
return helper(nums, 0, nums.length-1);
}
//concept: midpoint is the root/parent node and left part of array (smaller) is left node, right part be right node
//use recursion to keep on taking mid, left part, right part before left and right pointer overlap (no more elements)
public TreeNode helper(int[] nums, int start, int end){
if(start > end){
return null;
}
int mid = (start + end)/2;
TreeNode node = new TreeNode(nums[mid]);
node.left = helper(nums, start, mid-1);
node.right = helper(nums, mid+1, end);
return node;
}
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Convert Sorted Array to Binary Search Tree.
//Memory Usage: 44.1 MB, less than 20.12% of Java online submissions for Convert Sorted Array to Binary Search Tree.