Construct Binary Tree From Preorder And Inorder Traversal-(Microsoft, Amazon):Live Coding 🧑🏻‍💻

  Рет қаралды 10,526

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 31
@AIsinger96
@AIsinger96 7 ай бұрын
most underrated channel, I'll definitely post about this channel on linkedin.
@codestorywithMIK
@codestorywithMIK 7 ай бұрын
This means a lot ❤️❤️🙏🙏 Feel free to tag me on LinkedIn 😇
@AIsinger96
@AIsinger96 7 ай бұрын
@@codestorywithMIK yes bhaiya
@killeraloo3247
@killeraloo3247 7 ай бұрын
Bhai kya hi simple samjhate ho. Sab samajh aa jata hai.
@aaravmishra3487
@aaravmishra3487 Жыл бұрын
Different approach using 2 indices, one you mentioned in inorder & postorder one! class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTree(preorder, inorder, 0, inorder.length-1, 0, preorder.length-1); } public TreeNode buildTree(int[] preorder, int[] inorder, int inStart, int inEnd, int preStart, int preEnd) { if(inStart > inEnd || preStart > preEnd) return null; TreeNode root = new TreeNode(preorder[preStart]); // preorder = [3,2,1,4,5], root-> 3 (root, left, right) int i = inStart; for(; i
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Wonderful. So glad to see other approaches ❤️
@sauravfarkade1928
@sauravfarkade1928 5 ай бұрын
thanks a lot bhaiya.... sirf story se code likh liya... ;)
@muskanyadav8660
@muskanyadav8660 7 ай бұрын
great explanation...
@monjuralam7346
@monjuralam7346 Жыл бұрын
in the optimized version, no need to pass the inorder arry; we already have a map TreeNode* construct(vector& preorder, int l, int r, int& idx) {
@Lakshya-f4l
@Lakshya-f4l 4 ай бұрын
Beautiful question and great explanation!!
@keertilata20
@keertilata20 Жыл бұрын
SLIGHTLY OPTIMIZED APPROACH USING MAP TreeNode* solve(vector& preorder, vector& inorder, int start, int end, int &idx,map &inList ) { if(start>end){ return NULL; } int rootVal = preorder[idx]; int inorderIdx = inList[rootVal]; idx++; TreeNode* root = new TreeNode(rootVal); root->left = solve(preorder, inorder, start, inorderIdx-1, idx,inList); root->right = solve(preorder, inorder, inorderIdx+1, end, idx,inList); return root; } TreeNode* buildTree(vector& preorder, vector& inorder) { int idx = 0; int n = preorder.size(); map inList; for(int i=0;i
@SaranshKasliwal
@SaranshKasliwal 4 ай бұрын
Yeah using map Time-Complexity Will reduce to O(n) instead of O(n^2)
@aayushranjan5572
@aayushranjan5572 Жыл бұрын
bhaiya pls recursion padha do na us playlist me bus leetcode ke que sol krna plssssssssssss
@venkatarohitpotnuru38
@venkatarohitpotnuru38 4 ай бұрын
Super bhaiyya
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
Java Implementation: class Solution { private int idx; private TreeNode solve(int[] preorder, int[] inorder, int start, int end) { if(start > end) { return null; } TreeNode root = new TreeNode(preorder[idx]); int i = start; for(; i
@surajsidar2294
@surajsidar2294 7 ай бұрын
My below Java code is not passing example test cases I compared line by line . It looks same . What is wrong below class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { int idx = 0; return helper(preorder, inorder, 0, preorder.length - 1, idx); } TreeNode helper(int[] preorder, int[] inorder, int start, int end, int idx) { if (start > end) return null; int rootValue = preorder[idx]; int i = start; for (; i
@abhayroadlines528
@abhayroadlines528 3 ай бұрын
@@surajsidar2294 keep your variable "idx" out of the function.
@aditgulia
@aditgulia 6 ай бұрын
bal baccha karna hoga was epic line hahaha... btw nice explaination
@ayushkumarsingh6762
@ayushkumarsingh6762 9 күн бұрын
Genuine Doubt Please help 20:34 bhaiya hm log too idx++ kr rhe hai aur jb root->left & root->right mai too idx ka new value jayega too bina pass by reference k galat answer kyu aa rha hai ?
@rdrahuldhiman19
@rdrahuldhiman19 7 ай бұрын
Amazing man, thanks again.
@basujain8928
@basujain8928 5 ай бұрын
guys is wale ques vese striver soln jyada samajh aya mujhe
@codestorywithMIK
@codestorywithMIK 5 ай бұрын
Really glad to know you understood the solution , be it from any resource 😇❤️ And definitely I would be glad if you can share improvements or feedbacks.
@codeandtalk6
@codeandtalk6 Жыл бұрын
@shadabkalim9102
@shadabkalim9102 Жыл бұрын
hey, i used the approach from ur latest video of construction of binary tree using postorder and inorder and wrote this but its giving incorrect output: class Solution { public: TreeNode* solve(vector& inorder, vector& preorder, int inStart, int inEnd, int preStart, int preEnd, map& inMap) { if (preStart > preEnd || inStart > inEnd) return NULL; TreeNode* root = new TreeNode(preorder[preStart]); int i = inMap[root->val]; int leftSize = i - inStart; root->left = solve(inorder, preorder, inStart, i - 1, preStart + 1, preStart + leftSize, inMap); root->right = solve(inorder, preorder, i + 1, inEnd, preStart + leftSize + 1, preEnd, inMap); return root; } TreeNode* buildTree(vector& inorder, vector& preorder) { int n = inorder.size(); int inStart = 0, inEnd = n - 1; int preStart = 0, preEnd = n - 1; map inMap; for (int i = 0; i < n; i++) inMap[inorder[i]] = i; TreeNode* root = solve(inorder, preorder, inStart, inEnd, preStart, preEnd, inMap); return root; } }; can u pls tell me where i'm wrong? even chatgpt can't figure it out
@shadabkalim9102
@shadabkalim9102 Жыл бұрын
i figured out the error, it was a really silly mistake
@pixelpixel1977
@pixelpixel1977 7 ай бұрын
Why we need to pass the idx as pass by reference? Can someone please explain this once??
@surajsidar2294
@surajsidar2294 7 ай бұрын
There is need of pass by refernce. If you write same code in Java it won't work. Unlike C++ there is no pass by reference in Java , so there is need to create global variable idx. Below was my code in Java which didn't work class Solution { //int idx; public TreeNode buildTree(int[] preorder, int[] inorder) { int idx = 0; return helper(preorder, inorder, 0, preorder.length - 1,idx); } TreeNode helper(int[] preorder, int[] inorder, int start, int end,int idx) { if (start > end) return null; int rootValue = preorder[idx]; int i = start; for (; i
@PushpendraKushvaha
@PushpendraKushvaha 5 ай бұрын
@@surajsidar2294 Even mine is not working, have you find the solution for the same.
@Paul-ih8lj
@Paul-ih8lj 4 ай бұрын
If you don't pass it by reference, by default it'll be passed by value i.e each the recursive function gets called it'll be reset. By passing by reference you are able to remember the previous idx value.
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 88 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 44 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН
L33. Requirements needed to construct a Unique Binary Tree | Theory
8:41
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 834 М.