forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path_108.java
More file actions
25 lines (20 loc) · 643 Bytes
/
_108.java
File metadata and controls
25 lines (20 loc) · 643 Bytes
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
/**
* Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
*/
public class _108 {
public TreeNode sortedArrayToBST(int[] num) {
return rec(num, 0, num.length - 1);
}
public TreeNode rec(int[] num, int low, int high) {
if (low > high) {
return null;
}
int mid = low + (high - low) / 2;
TreeNode root = new TreeNode(num[mid]);
root.left = rec(num, low, mid - 1);
root.right = rec(num, mid + 1, high);
return root;
}
}