13.24 CONSTRUCT BINARY TREE FROM INORDER AND PREORDER(Tree)-Interviewbit #tree#programming#bst

preview_player
Показать описание
#trees#tree#bst#binarytree#interview#algorithm#datastrucutres#programming#interviewbit#coding#code#coding #programming #programmer #code #python #coder #technology #codinglife #cpp #c #tech #computerscience #software #softwaredeveloper #softwareengineer #programmers #bhfyp

this video consists of topic form interview bit tree and further videos will be uploaded soon. till then for any doubts write in the comment section.

don't forget to SUBSCRIBE.
Рекомендации по теме
Комментарии
Автор

Literally very helpful. Thank you so much

pepetikesavasiddhardha
Автор

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int i=0;
TreeNode* create(vector<int>&pre, vector<int>&in, int s, int e){


if(s>e){
return NULL;
}

TreeNode*root = new TreeNode(pre[i]);

int idx = -1;
for(int j=s;j<=e;j++){
if(in[j]==pre[i]){
idx = j;
break;
}
}

i++;
root->left = create(pre, in, s, idx-1);
root->right = create(pre, in, idx+1, e);

return root;

}


TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
int index = 0;
TreeNode* temp = create(preorder, inorder, index, n-1);
return temp;
}
};


why is this giving wrong answer on interviewbit while its correct on leetcode???

saurav