Easy to Understand Code Submission


#1

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    TreeNode
    createBalancedTree(const vector &A,int start,int end)
    {
    if(start>end){
    return NULL;
    }
    int mid = (start+end)/2;
    TreeNode ptr = new TreeNode(A[mid]);
    ptr->left = createBalancedTree(A,start,mid-1);
    ptr->right = createBalancedTree(A,mid+1,end);
    return ptr;
    }
    TreeNode
    Solution::sortedArrayToBST(const vector &A) {
    if(A.size()==0){
    return NULL;
    }
    TreeNode *ptr = createBalancedTree(A,0,A.size()-1);
    return ptr;
    }