码迷,mamicode.com
首页 > 其他好文 > 详细

Convert Sorted Array to Binary Search Tree

时间:2014-07-21 00:11:12      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode sortedArrayToBST(int[] num) {
        if(num.length<=0) return null;
        
        TreeNode root=sort(num,0,num.length-1);
        return root;
        
        
    }
    public TreeNode sort(int[] num,int i,int j)
    {
        if(i>j) return null;
        if(i==j)
        {
            return new TreeNode(num[i]);
        }
        int mid=(i+j)>>1;
        TreeNode root=new TreeNode(num[mid]);
        root.left=sort(num,i,mid-1);
        root.right=sort(num,mid+1,j);
        
        return root;
        
        
        
        
    }
}

Convert Sorted Array to Binary Search Tree,布布扣,bubuko.com

Convert Sorted Array to Binary Search Tree

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/hansongjiang/p/3857303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!