美文网首页
Java日记2018-07-21

Java日记2018-07-21

作者: hayes0420 | 来源:发表于2018-07-21 06:54 被阅读0次

今天回老家,这个周末看书多一点
将有序的数组转变为二叉搜索树

public static TreeNode areverse(int[] arr){
        if(arr==null) return null;
        return recore(arr,0,arr.length-1);
    }
    
    public static TreeNode recore(int[] arr, int start, int end) {
        if (arr.length == 0)
            return null;
        TreeNode root;
        System.out.println("start:"+start+" end:"+end);
        if (start == end) {
            root = new TreeNode(arr[start]);
            root.left = null;
            root.right = null;
        } else {
            int mid = (start + end) / 2;
            //System.out.println(mid);
            root = new TreeNode(arr[mid]);
            root.left = recore(arr, start, mid - 1);
            root.right = recore(arr, mid + 1, end);
        }
        return root;
    }

相关文章

网友评论

      本文标题:Java日记2018-07-21

      本文链接:https://www.haomeiwen.com/subject/djbcmftx.html