(将有序数组转成平衡二叉树)LeetCode#108. Convert Sorted Array to Binary Search Tree

  • 题目:给定有序数组,将有序数组转成二叉平衡树
  • 难度:Easy
  • 思路:根据二叉平衡树的性质,可采用二分查找来确定节点
  • 代码:
public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums == null || nums.length == 0){
            return null;
        }
        return helper(nums, 0, nums.length-1);
    }
    public TreeNode helper(int[] nums, int left, int right){
        if(left <= right){
            int mid = left + (right-left)/2;
            TreeNode node = new TreeNode(nums[mid]);
            node.left = helper(nums, left, mid-1);
            node.right = helper(nums, mid+1, right);
            return node;
        }else{
            return null;
        }
    }
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/u012559634/article/details/72876699
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞