forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_108.java
29 lines (24 loc) · 791 Bytes
/
_108.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
/**
* 108. Convert Sorted Array to Binary Search Tree
*
* Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
*/
public class _108 {
public static class Solution1 {
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;
}
}
}