Link to code-> https://ide.codingblocks.com/s/646033
Link to question-> https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
please help
Link to code-> https://ide.codingblocks.com/s/646033
Link to question-> https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
please help
hi @ikshuldureja130
refer this -->
TreeNode* bstFromPreorder1(vector<int>& preorder) {
int pos =0;
return bst(preorder, pos, INT_MAX, INT_MIN);
}
TreeNode*bst(vector<int>&preorder, int &pos, int max, int min){
if (pos>=preorder.size()) return NULL;
int val = preorder[pos];
if (val > max || val <min) return NULL;
++pos;
TreeNode* node = new TreeNode(val);
node->left = bst(preorder, pos, val, min);
node->right = bst(preorder, pos, max, val);
return node;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
My code is passing only 1 test case…please check
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.