Construct Binary Tree from Inorder and Preorder Traversal | Love Babbar DSA Sheet | Amazon 🔥 | GFG
#tree #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Construct Binary Tree from Inorder and Preorder Traversal'.
Array question Playlist = • Love Babbar DSA 450 Qu...
String question Playlist = • Love Babbar DSA 450 Qu...
Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
RoadMap of Web Development = • 🔴 Web Development Road...
Roadmap for Dynamic Programming = • Complete Roadmap for D...
Great Strategy to solve DSA = • Great Strategy to solv...
My Journey to 5 star at codechef = • My Journey to 5 Star a...
Love Babbar DSA Sheet : drive.google.com/file/d/1FMdN...
Follow us on Instagram:
Shailesh Yogendra : / shaileshyogendra
Yogesh Yogendra : / i_am_yogesh_here
Follow us on LinkedIn:
Yogesh Yogendra : / yogesh-yogendra-26bbb518a
Shailesh Yogendra : / shailesh-yogendra-8b13...
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
Пікірлер: 56
Thank you so much for these videos. You have helped an unimaginable deal to give me the zeal to pretty much start coding. I usually don't comment at all, but you totally deserve it. I can't thank you enough!
kasam se bro...pichle 10 ghante se samajhne ki try kr rha ..tysm for this explanation.
The most important reason for which i was stuck with this was an assumption I missed, which was of not assuming duplicates... If there were two nodes with same value, searching or using map would be futile.. Would appreciate if anyone could find the solution where duplicates could be allowed
calling nodes as "Bande Log" is awesome
@RajputAnkit11
2 жыл бұрын
haha
Bro..I am continuously doing dsa450 sheet , is it usefull in cracking Interview rounds ..please tell bro..🙏
Thanks bro nice explanation, and keep solving the sheet questions👏
bhai acha smj aya ..thanks for this video
Thanks for the video!
I always watch your video trust me u r d best..... I have a doubt though why you did m.clear() and also idx = 0(you had already declared it in line number 63) in line number 81 and 80 respectively. 😊😊😊😊😊😊😊😊
@CodeLibrary
3 жыл бұрын
when i was solving i thought that the T is also given i.e testcases ..... so for each testcases you have to make idx=0 and clear map because we have taken both of them globally....... If T is not given then no need to do that
@krishanudev4023
3 жыл бұрын
@@CodeLibrary 🙏🙏
u r so good in teaching
Thank u so much... very useful
AMAZING EXPLANATION
Amazing 🔥
nice explanation bro
y are u passing inorder array if map is being used
Will it work for duplicate values?
thanks!
You need to add a condition at 66 to check if idx doesnt exceed ub limit. if(lb > ub || idx >= ub) return null; this has to be added
@rajatmaheshwari4656
2 жыл бұрын
it's still working without the condition in gfg
isme distinct toh mention nhi toh unordered map fail nhi karega repeating element k liye?
@livelypooja
Жыл бұрын
Haan submit ni hoga code.maine isliye search Kiya element in order me frm lb to ub
bro i think if (lb == ub) condition is redundent....
Samaj nahi aaya par sunkar achha laga 😂😂
@AinasDiaries
2 жыл бұрын
+1
it is memorized solution not more than that
Writing the code from scratch would be of great help rather than explaining the code what is written already
it is showing segmentation fault!!
Map tabhi kaam ayega jab all the nodes have distinct values .
Code of the video: TreeNode* solve(vector& preorder, vector& inorder, int start, int end) { if (start > end) return nullptr; TreeNode* node = new TreeNode(preorder[index++]); int mid = mp[node->val]; node->left = solve(preorder, inorder, start, mid - 1); node->right = solve(preorder, inorder, mid + 1, end); return node; } TreeNode* buildTree(vector& preorder, vector& inorder) { int n = preorder.size(); for (int i = 0; i mp[inorder[i]] = i; } return solve(preorder, inorder, 0, n - 1); }
Bhai bhut shaandar
Bhai please bta do idx kyun global lia h, jahan global nhi lia wahan with reference lia h. Mai 5 days se ispr atka hua hu please tell me
@CodeLibrary
Жыл бұрын
yaa both options are correct. You can take global or by reference as well.
@mirdulswarup9065
Жыл бұрын
@@CodeLibrary bhai please please yeh bta do local variable without reference kyun ni le skte
bhaiya paar inorder to humesa sorted form mai hota hai??
@sanketgharatkar5379
2 жыл бұрын
bhai vo sirif BST me hota haii
@yashgupta-fk3zc
2 жыл бұрын
@@sanketgharatkar5379 thnx bro
Can anyone tell me Why idx has to be declared globally? And if sent as arguments then why its sent as reference?
@ankitdwivedi4213
2 жыл бұрын
because we need to update the value of the variable in each recursion stack, so address is passed .
Using map will not pass the test cases with duplicate value:- Try this code:- int idx=0; int search(int in[],int k,int l){ for(int i=l;1;i++) if(k==in[i]) return i; } Node* buildTree1(int in[],int pre[],int l,int r){ if(l>r) return NULL; Node* p=new Node(pre[idx++]); if(l==r) return p; int mid=search(in,p->data,l); p->left=buildTree1(in,pre,l,mid-1); p->right=buildTree1(in,pre,mid+1,r); return p; } Node* buildTree(int in[],int pre[], int n) { // Code here return buildTree1(in,pre,0,n-1); }
akshay kumar meme at 6:28 😃
idk whats wrong with geek for geek , it gives error after 60 test cases
@bhakodiasolankiakash4312
11 ай бұрын
It's probably because the test case is having duplicate node
@jatinkashyap9986
Ай бұрын
Instead of using map use that in[] and make a function which will find the value of that node from that In array and return the position
run time error aa rha hai
tough one:
apne submit bhi nhi krke dekhaya
@CodeLibrary
3 жыл бұрын
Watch till end....I have submitted the Solution..have a look
if(start==end) return root; This line is unnecessary
it is showing segmentation fault!!
@yashvardhan6001
2 жыл бұрын
bro yeh problem kaise solve hui