two C++


  • 0
    S
    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            TreeNode* root;
            BST(root,nums,0,nums.size());
            return root;
        }
    private:
        void BST(TreeNode*& root,vector<int>& nums,int s,int e)
        {
            if(s>=e)
                root = NULL;
            else
            {
                int mid = (s+e)/2;
                root = new TreeNode(nums[mid]);
                BST(root->left,nums,s,mid);
                BST(root->right,nums,mid+1,e);
            }
        }
    };
    
    
    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return BST(nums,0,nums.size()-1);
        }
    private:
        TreeNode* BST(vector<int>& nums, int s,int e)
        {
            if(s>e)
                return NULL;
            int mid = (s+e)/2;
            TreeNode* root = new TreeNode(nums[mid]);
            root->left = BST(nums,s,mid-1);
            root->right = BST(nums,mid+1,e);
            return root;
        }
    };

Log in to reply
 

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