/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST2(int[] num, int first, int last){
if(last < first)return null;
int middle = (first+last+1)/2;
TreeNode head = new TreeNode(num[middle]);
head.left = sortedArrayToBST2(num, first, middle - 1);
head.right = sortedArrayToBST2(num, middle + 1, last);
return head;
}
public TreeNode sortedArrayToBST(int[] num) {
return sortedArrayToBST2(num, 0 , num.length-1);
}
}
Convert Sorted Array to Binary Search Tree
最新推荐文章于 2022-02-19 22:56:38 发布