一、题目
二、代码
/**
* 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 dfs(nums,0,nums.length-1);
}
public TreeNode dfs(int[] nums , int low ,int high)
{
if(low>high) return null;
int mid = low+(high - low)/2;
TreeNode cur = new TreeNode(nums[mid]);
cur.left = dfs(nums,low,mid-1);
cur.right = dfs(nums,mid+1,high);
return cur;
}
}