Skip to content

Commit e4679dc

Browse files
authored
Create Day24.cpp
1 parent a0ff722 commit e4679dc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Day24.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Author: Aryan Yadav
3+
Construct Binary Search Tree from Preorder Traversal
4+
5+
Algorithm: Traversal in tree
6+
Difficulty: Medium
7+
*/
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* struct TreeNode {
12+
* int val;
13+
* TreeNode *left;
14+
* TreeNode *right;
15+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
int i = 0;
23+
TreeNode* bstFromPreorder(vector<int>& preorder,int bound = INT_MAX) {
24+
if (i == preorder.size() || preorder[i] > bound) return NULL;
25+
TreeNode* root = new TreeNode(preorder[i++]);
26+
root->left = bstFromPreorder(preorder, root->val);
27+
root->right = bstFromPreorder(preorder, bound);
28+
return root;
29+
}
30+
};

0 commit comments

Comments
 (0)