/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode sortedArrayToBST(int[] nums) { TreeNode root; root=f(nums,0,nums.length-1); return root; } public TreeNode f(int[] nums,int x,int y){ if(x>y) return null; TreeNode root=new TreeNode(); int s=(x+y)/2; root.val=nums[s]; root.left=f(nums,x,s-1); root.right=f(nums,s+1,y); return root; } }
给定一个有序整数数组,元素各不相同且按升序排列,编写一个算法,创建一棵高度最小的二叉搜索树
最新推荐文章于 2023-10-16 15:47:11 发布