Skip to content

Commit 928a0db

Browse files
committed
add cpp solution
1 parent e19acfd commit 928a0db

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
TreeNode* construct(int i, int j, vector<int>& preorder){
4+
if (i > j) return NULL;
5+
TreeNode* root = new TreeNode(preorder[i]);
6+
int k = i;
7+
while (k <= j and preorder[k] <= preorder[i]) k++;
8+
root->left = construct(i+1, k-1, preorder);
9+
root->right = construct(k, j, preorder);
10+
return root;
11+
}
12+
13+
TreeNode* bstFromPreorder(vector<int>& preorder) {
14+
return construct(0, preorder.size() - 1, preorder);
15+
}
16+
};

0 commit comments

Comments
 (0)