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);
}
}
将升序数组转化为平衡二叉树
最新推荐文章于 2023-12-11 19:54:24 发布