标签:
public class Solution { public TreeNode sortedArrayToBST(int[] nums) { int len=nums.length; TreeNode res=buildNode(0,len-1,nums); return res; } public TreeNode buildNode(int l,int r,int[] nums) { //使用递归的方式,终止条件是 左游标大于右游标 if(l>r) { return null; } // 将左右值相等从终止结束条件中独立出来 int mid=(l+r)/2; TreeNode temp=new TreeNode(nums[mid]); TreeNode left=buildNode(l,mid-1,nums); TreeNode right=buildNode(mid+1,r,nums); temp.left=left; temp.right=right; return temp; } }
108. Convert Sorted Array to Binary Search Tree
标签:
原文地址:http://www.cnblogs.com/aguai1992/p/5353063.html