Java recursive code very intuitive


  • 0
    S

    class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
    return makeTree(nums,0,nums.length-1);
    }
    private TreeNode makeTree(int[] nums,int si, int ei){
    if(si<=ei){
    int mid = si+(ei-si)/2;
    TreeNode root = new TreeNode(nums[mid]);
    root.left = makeTree(nums,si,mid-1);
    root.right = makeTree(nums,mid+1,ei);
    return root;
    }
    return null;
    }
    }


Log in to reply
 

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