I feel that the answer is not unique.


  • 0
    X

    I feel that the answer is not unique.
    when Input is [1, 5, 7, 9, 10], the following code output is [7,1,9,null,5,null,10], and expect answer is [7,5,10,1,null,9]
    I think both of them are correct?

     */
    class Solution {
    public:
        TreeNode * DFS(vector<int> & nums, int s, int e){
            if(s > e) return NULL;
            int m = (e+s)/2;
            TreeNode *root = new TreeNode(nums[m]);
            root->left = DFS(nums, s, m-1);
            root->right = DFS(nums, m+1, e);
            return root;
        }
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            if(nums.empty()) return NULL;
            return DFS(nums, 0, nums.size()-1);
        }
    };
    

Log in to reply
 

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