You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
620 B

package class30;
public class Problem_0108_ConvertSortedArrayToBinarySearchTree {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public TreeNode sortedArrayToBST(int[] nums) {
return process(nums, 0, nums.length - 1);
}
public static TreeNode process(int[] nums, int L, int R) {
if (L > R) {
return null;
}
if (L == R) {
return new TreeNode(nums[L]);
}
int M = (L + R) / 2;
TreeNode head = new TreeNode(nums[M]);
head.left = process(nums, L, M - 1);
head.right = process(nums, M + 1, R);
return head;
}
}