java 108.将Sorted Array转换为二进制搜索Tree.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 108.将Sorted Array转换为二进制搜索Tree.java相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums == null) return null;
        return helper(nums,0, nums.length - 1);
    }
    public TreeNode helper(int[] nums, int start, int end){
        if (start > end){
            return null;
        }
        
        int middle = (start + end) / 2;
        TreeNode n = new TreeNode(nums[middle]);
        n.left = helper(nums, start, middle - 1);
        n.right = helper(nums, middle + 1, end);
        return n;
    }
}

以上是关于java 108.将Sorted Array转换为二进制搜索Tree.java的主要内容,如果未能解决你的问题,请参考以下文章

java 108.将Sorted Array转换为二进制搜索Tree.java

java 108.将Sorted Array转换为二进制搜索Tree.java

java 108.将Sorted Array转换为二进制搜索Tree.java

java 108.将Sorted Array转换为二进制搜索Tree.java

c_cpp 108.将Sorted Array转换为二进制搜索树

108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树