Simple C++ Recursive Solution


  • 0
    M
    class Solution {
    public:
        TreeNode* SolveRec(int const * begin, int const * end) {
            if (begin >= end) return 0;
            int const * mid = begin + (end - begin) / 2;
            TreeNode * result = new TreeNode(*mid);
            result->left = SolveRec(begin, mid);
            result->right = SolveRec(mid + 1, end);
            return result;
        }
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return SolveRec(nums.data(), nums.data() + nums.size());
        }
    };
    

Log in to reply
 

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