Easy to understand


  • 0
    S
    class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            return buildTree(nums, 0, nums.length-1);
        }
        
        public TreeNode buildTree(int nums[], int i, int j) {
            if(i==j)
                return new TreeNode( nums[i] );
            if(i<j) {
                int m = (i+j)/2;
                TreeNode root = new TreeNode( nums[m] );
                root.left = buildTree( nums, i, m-1 );
                root.right = buildTree( nums, m+1, j );
                return root;
            }
            return null;
        }   
    }
    

Log in to reply
 

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