14 lines C++ solution


  • 1
    Z
    class Solution {
        public:
            TreeNode *sortedArrayToBST(vector<int> &num) {
                return build(num, 0, num.size()-1);
            }
            TreeNode *build(vector<int> &num, int begin, int end){
                if(begin>end)   return NULL;
                int mid = (begin + end)/2;
                TreeNode *n = new TreeNode(num[mid]);
                n->left = build(num, begin, mid-1);
                n->right = build(num, mid+1, end);
                return n;
            }
    };

  • 0
    B
    This post is deleted!

  • 0
    B

    why is num.size()-1 instead of num.size() in line 4? I didn't see anything bad but it response me an error in arrays.


  • 0
    B
    This post is deleted!

  • 0
    D

    it will exceed the index range


Log in to reply
 

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