Skip to content

Commit a56ea08

Browse files
committed
leetcode-30-day-leetcoding-challenge-530-week-3-3305.cpp
1 parent 327978d commit a56ea08

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
TreeNode* bstFromPreorder(vector<int>& preorder) {
2+
int n=preorder.size();
3+
return bsthelper(preorder,0,n);
4+
}
5+
TreeNode * bsthelper(vector<int>preorder,int s,int n){
6+
if(s>=n){
7+
return NULL;
8+
}
9+
int data=preorder[s];
10+
TreeNode * root=new TreeNode(data);
11+
int i=s+1;
12+
while(i<preorder.size() && preorder[i]<data){
13+
i++;
14+
}
15+
root->left=bsthelper(preorder,s+1,i);
16+
root->right=bsthelper(preorder,i,n);
17+
return root;
18+
}
19+
};

0 commit comments

Comments
 (0)