Easy understandable recursive way in JAVA


  • 0
    D

    this is a easy understandable way similar to the 109 problem

    public class Solution {

    private int i=0;
    public TreeNode sortedArrayToBST(int[] nums) {
        int len=nums.length;
        return function(nums,0,len-1);
    }
    public TreeNode function(int[] nums,int start,int end){
        if(start>end) return null;
        int mid=start+(end-start)/2;
        TreeNode left=function(nums,start,mid-1);
        TreeNode tree=new TreeNode(nums[i]);
        tree.left=left;
        i++;
        tree.right=function(nums,mid+1,end);
        return tree;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.