Short C++ recursion

  • 0
        TreeNode* sortedArrayToBST(vector<int>& a) {
            return dfs(a.begin(), a.end());
        TreeNode* dfs(vector<int>::iterator i, vector<int>::iterator j) {
            if (i == j) return nullptr;
            auto mid = i + (j-i)/2;
            auto r = new TreeNode(*mid);
            r->left = dfs(i, mid), r->right = dfs(mid+1, j);
            return r;

Log in to reply

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