Why it shows runtime error when using my recursive code?


  • 1
        /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode *sortedArrayToBST(vector<int> &num, int start, int end) {
            if (start > end) {
                return NULL;
            } 
            int middle = start + (end - start) >> 1;
            TreeNode *p = new TreeNode(num[middle]);
            if (start != end) {
                p->left = sortedArrayToBST(num, start, middle - 1);
                p->right = sortedArrayToBST(num, middle + 1, end);
            }
            return p;
        }
        TreeNode *sortedArrayToBST(vector<int> &num) {
            //int len = (int)num.size();
            //if (len == 0) return NULL;
            return sortedArrayToBST(num, 0, num.size() - 1);
        }
    };

  • 1

    I see...
    I made a rediculous mistake that the priority of “>>" is weaker than '+' operator


Log in to reply
 

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