首页 > 其他 > 详细

108. Convert Sorted Array to Binary Search Tree

时间:2016-06-10 06:15:04      阅读:173      评论:0      收藏:0      [点我收藏+]

和104,105一个方法

 1     public TreeNode sortedArrayToBST(int[] nums) {
 2         if(nums.length == 0) {
 3             return null;
 4         } 
 5         return helper(nums, 0, nums.length - 1);
 6     }
 7     
 8     private TreeNode helper(int[] nums, int start, int end) {
 9         if(start > end) {
10             return null;
11         }
12         int rootIndex = start + (end - start) / 2;
13         TreeNode root = new TreeNode(nums[rootIndex]);
14         root.left = helper(nums, start, rootIndex - 1);
15         root.right = helper(nums, rootIndex + 1, end);
16         return root;
17     }

 

108. Convert Sorted Array to Binary Search Tree

原文:http://www.cnblogs.com/warmland/p/5573113.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!