将升序数组转化为平衡二叉树

public class Solution {
    TreeNode convert(int num[], int l, int r) {
        if (l >= r) return null;
        int m = (l + r) / 2;
        TreeNode left = convert(num, l, m);
        TreeNode root = new TreeNode(num[m]);
        TreeNode right = convert(num, m + 1, r);
        root.left = left;
        root.right = right;
        return root;
    }

    public TreeNode sortedArrayToBST(int[] num) {
        return convert(num, 0, num.length);
    }
}

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/wongson/article/details/45370957
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞