Balance a Binary Search Tree | Simple Explanation | Leetcode 1382 | codestorywithMIK

  Рет қаралды 7,776

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 80
@ankitrajpootr
@ankitrajpootr 6 ай бұрын
Today is my Birthday🎉.
@venkatarohitpotnuru38
@venkatarohitpotnuru38 6 ай бұрын
happy birthday bro...
@codestorywithMIK
@codestorywithMIK 6 ай бұрын
Wish you a very very Happy Birthday Ankit ❤️❤️❤️ Guys let’s all wish him and make his day 😇🎉🎊🎁🎈🎂
@AbhijeetMuneshwar
@AbhijeetMuneshwar 6 ай бұрын
Happy Birthday, Ankit 🍰🎉
@varunpalsingh3822
@varunpalsingh3822 6 ай бұрын
Happy Birthday bro, Enjoy your day 🎊🎉
@Himanshh_29
@Himanshh_29 6 ай бұрын
Happy Birthday Ankit 🥳
@AbhijeetMuneshwar
@AbhijeetMuneshwar 6 ай бұрын
Respected MIK Sir, Leetcode publishes new problem of the day at 5:30 AM daily. You've published this video just after an hour. Hats off to your dedication and efforts. 🙏🙇 You're an inspiration ⚡
@codestorywithMIK
@codestorywithMIK 6 ай бұрын
Means a lot ❤️🙏
@vishalkurve93
@vishalkurve93 6 ай бұрын
​@@codestorywithMIKReally sir you are helping us a lot🙌🙌🙌🥹
@gui-codes
@gui-codes 6 ай бұрын
Banda legend hai yaar. Itna determination chaie bas life me.
@kaustubhchaudhari9838
@kaustubhchaudhari9838 6 ай бұрын
First shortest video of MIK bhai ever seen
@rajatrajgautam3224
@rajatrajgautam3224 6 ай бұрын
but bhai worth it h
@gui-codes
@gui-codes 6 ай бұрын
ha ha sahi baat. but video is gem as always.
@molyoxide8358
@molyoxide8358 6 ай бұрын
Go see his shorts, even more shorter. Just joking buddy.
@adsnehi
@adsnehi 6 ай бұрын
I am not able to solve any daily challenge problem. but after watching 25% of you video I am able to solve it
@krunalkp1032
@krunalkp1032 Ай бұрын
Hi Mik you are really doing a great job you way of teaching is really nice, I code in python hence i watch your all video till the coding part and am able to do it by my self. Thanks much recursion is indeed work magically. Python Code : class Solution: def balanceBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: arr = [] def inorder(root): nonlocal arr if root is None: return inorder(root.left) arr.append(root.val) inorder(root.right) inorder(root) def makebalance(left,right,arr): if left > right: return None mid = left + (right - left) // 2 root = TreeNode(arr[mid]) root.left = makebalance(left,mid-1,arr) root.right = makebalance(mid+1,right,arr) return root left = 0 right = len(arr) - 1 return makebalance(left,right,arr)
@rohan8758
@rohan8758 5 ай бұрын
Awesome explanation bhaiyan, You are like a angel to me who is leading me towards to crack FAANG or product based companies interview.
@Lakshya-f4l
@Lakshya-f4l 6 ай бұрын
"Recursion leap of faith" . . . Thankyou sir!
@abhinaysahuu
@abhinaysahuu 6 ай бұрын
sir aapki explanations really the best h. mene ek striver ki binary tree ki video dekhi, smjh ni aaya code, aapki video dekhi, easily aa gya. please keep up the good work sir !
@manasdeora4601
@manasdeora4601 6 ай бұрын
Sir, you have been helping many like me understand the concepts in deep. Thankyou so much for your efforts.
@CarWarYaar
@CarWarYaar 6 ай бұрын
Bhaiya aap bhot acha samjhate ho , dsa ke saare patterns pe koi video bana dijiye , question identify krne me problem hoti hai mostly
@Abhishek-y6i3j
@Abhishek-y6i3j 6 ай бұрын
short and crisp explanation thanks for this .
@manojitsaha6262
@manojitsaha6262 6 ай бұрын
AVL Tree 🙂 Mujhe college ka kuch samajh nahi ata apke video se samjha ye concept acche se ❤❤❤
@raunakgiri5033
@raunakgiri5033 6 ай бұрын
It would be very helpful, if you also provide contest video solution, specially only problem D. Thanks.
@gauravbanerjee2898
@gauravbanerjee2898 6 ай бұрын
Thanks a lot bhaiya ❤❤
@ReshuSharma-jc7yu
@ReshuSharma-jc7yu 6 ай бұрын
Bhaiya your segment tree playlist is awesome and looking forward for your backtracking and heap playlist
@meesalamahesheee_0056
@meesalamahesheee_0056 6 ай бұрын
Sir, how did you gain so much knowledge and become consistent in DSA? What experiences and difficulties did you face during your initial stages in DSA, please tell?
@aws_handles
@aws_handles 6 ай бұрын
Guruji 🙌🙏🏻
@gui-codes
@gui-codes 6 ай бұрын
MIK bhai zinda baad
@bhuppidhamii
@bhuppidhamii 6 ай бұрын
East ho ya West, MIK is best.
@venkatarohitpotnuru38
@venkatarohitpotnuru38 6 ай бұрын
bhaiyya please do problems on partition dp
@codestorywithMIK
@codestorywithMIK 6 ай бұрын
Soon planning in dp concepts playlist ❤️
@nish0798
@nish0798 6 ай бұрын
@@codestorywithMIK also please try to give tips how to be good at bottomup and when are u planning to do dp concepts and how amny videos estimated
@venkatarohitpotnuru38
@venkatarohitpotnuru38 6 ай бұрын
@@codestorywithMIK Thank you bhaiyya..
@ropurifiedwater
@ropurifiedwater 6 ай бұрын
@@codestorywithMIK thank you sir
@FanIQQuiz
@FanIQQuiz 6 ай бұрын
Resursion ❌ Kahani (story) ✅
@aizad786iqbal
@aizad786iqbal 6 ай бұрын
awesome
@dayashankarlakhotia4943
@dayashankarlakhotia4943 6 ай бұрын
Good explanation 👏 👍
@AdarshSingh-is6mg
@AdarshSingh-is6mg 6 ай бұрын
bhaiya aap sheet recommend krskte ho koi for interview prep.
@Coder_Buzz07
@Coder_Buzz07 6 ай бұрын
"College mein yeh problem karaya hoga" - college mein kuch nhi karata bhaiya sab khudse karte hai hum log😂😂
@kartikforwork
@kartikforwork 6 ай бұрын
karwata hai na clg, maths, phy, chem, dip, human values, tecnical communication ... me time waste
@Coder_Buzz07
@Coder_Buzz07 6 ай бұрын
@@kartikforwork 😂🤣🤣sahi bole bhai
@imPriyansh77
@imPriyansh77 6 ай бұрын
Thank you bhaiya
@jeehub041
@jeehub041 6 ай бұрын
Sir jo new tree hum log construct kar rahe usko bhi toh space complexity me consider karenge na?
@pokeindia5361
@pokeindia5361 6 ай бұрын
Bhaiya please wo palindromic substringa ka blue print aapne diya tha uske variant karwao please..... Abhi placements aa rahe h
@dhairyachauhan6622
@dhairyachauhan6622 6 ай бұрын
This is how i did it. was wondering, how to do it inplace, i know AVL TREE but never implement the rotation. Even after being decent in recursion, it is difficult for me to implement it 😅 /** * 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: vectorinorder; void fillInorder(TreeNode* root){ if(root == NULL){ return; } fillInorder(root->left); inorder.push_back(root->val); fillInorder(root->right); } TreeNode* makeTree(TreeNode*root,int start,int end){ if(start > end){ return NULL; } int mid = start + (end-start)/2; TreeNode * node = new TreeNode(inorder[mid]); node->left = makeTree(root,start,mid-1); node->right = makeTree(root,mid+1,end); return node; } TreeNode* balanceBST(TreeNode* root) { fillInorder(root); return makeTree(root,0,inorder.size()-1); } };
@rhydhamsingla9597
@rhydhamsingla9597 6 ай бұрын
Bhaiya please make a separate video on DSW algorithm. I wanted to learn it and there is no video tutorial available. I checked those which were available, they don't explain any near to you. Please help!
@bhaktisharma9681
@bhaktisharma9681 6 ай бұрын
Mujhe BST ki playlist he nahi mill rahi hai, koi pls share kar sakt hai uska link?
@piyush1394
@piyush1394 6 ай бұрын
class Solution { public: TreeNode* balancedTreeRoot(int l, int h, vector &arr){ if(l > h){ return NULL; } int mid = l + (h-l)/2; TreeNode* root = new TreeNode(arr[mid]); root->left = balancedTreeRoot(l, mid-1, arr); root->right = balancedTreeRoot(mid+1, h, arr); return root; } void createArray(TreeNode* root, vector &vec){ if(root == nullptr) return; createArray(root->left, vec); vec.push_back(root->val); createArray(root->right, vec); } TreeNode* balanceBST(TreeNode* root) { vector arr; createArray(root, arr); return balancedTreeRoot(0,arr.size()-1, arr); } };
@sabbyasachichakraborty1824
@sabbyasachichakraborty1824 6 ай бұрын
LC 3193 kara do pls. Baaki jagah se samjh hi nhi aa rha. Count inversion wala
@rahulnegi4027
@rahulnegi4027 6 ай бұрын
us bro us
@focusman8801
@focusman8801 6 ай бұрын
@codestorywithMIK Bhaiya please count inversion wala bana do
@10minutes_cs
@10minutes_cs 6 ай бұрын
yes please, male video on this.
@__ankush_kushwaha
@__ankush_kushwaha 6 ай бұрын
❤❤❤
@top_g755
@top_g755 6 ай бұрын
Sir segment tree plz
@Rajdweep
@Rajdweep 6 ай бұрын
man i didn't get the intuition for this
@yogeshchauhan9401
@yogeshchauhan9401 6 ай бұрын
In the if u are checking nums[idx]
@theUnkown617
@theUnkown617 6 ай бұрын
arre bhaiya DSW bhi padha dete na 😭 me vohi padhne aya tha pr apne dokha de diya
@mayank1522
@mayank1522 6 ай бұрын
yaha par l > r kese hoga , l increase kese ho rha h ya r decrease kese ho rha
@yogeshchauhan9401
@yogeshchauhan9401 6 ай бұрын
We are recursively passing them in the function like for left subtree construct(l,mid-1) and for right subtree(mid+1,r) and as a base case we are checking is l>r that means there is no more element to process so we return NULL.
@ManojKrVerma-vw4dx
@ManojKrVerma-vw4dx 6 ай бұрын
What is the issue with this code ? /** * 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: void helper(TreeNode* root, vector &sorted) { if (root == NULL) return; helper(root->left, sorted); sorted.push_back(root); helper(root->right, sorted); } TreeNode* balancedBST(vector sorted, int l, int h) { if (l > h) return NULL; if (l==h){ return sorted[l]; } int m = (h+l) / 2; TreeNode* root = sorted[m]; root->left = balancedBST(sorted, l, m - 1); root->right = balancedBST(sorted, m + 1, h); return root; } TreeNode* balanceBST(TreeNode* root) { // first get sorted vector vector sorted; helper(root, sorted); //return NULL; return balancedBST(sorted, 0, sorted.size()-1); } };
@nish0798
@nish0798 6 ай бұрын
public int subarraySum(int[] nums, int k) { // Call the helper function with an initial sum of 0 return helper(nums, k, 0); } private int helper(int[] nums, int k, int idx) { if(idx>=nums.length) { if(k==0) { return 1; } return 0; } int take=0; if(nums[idx]
@nish0798
@nish0798 6 ай бұрын
@codestorywithMIK please reply
@molyoxide8358
@molyoxide8358 6 ай бұрын
Bro my code is not running pls help me: class Solution { public: int n = 0; vector arr; TreeNode* newRoot = NULL; TreeNode* temp = NULL; void SolveLeft(int l, int r, vector &arr) { if(lleft = new TreeNode(arr[mid]); temp = temp->left; SolveLeft(l, mid-1, arr); } } void SolveRight(int l, int r, vector &arr) { if(lright = new TreeNode(arr[mid]); temp = temp->right; SolveRight(mid+1, n-1, arr); } } void Inorder(TreeNode* root) { if(root != NULL) { Inorder(root->left); arr.push_back(root->val); Inorder(root->right); } } TreeNode* balanceBST(TreeNode* root) { Inorder(root); n = size(arr); int mid = (0+n-1)/2; newRoot = new TreeNode(arr[mid]); temp = newRoot; SolveLeft(0, mid-1, arr); temp = newRoot; SolveRight(mid+1, n-1, arr); return newRoot; } };
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 277 М.
AVL Trees & Rotations (Self-Balancing Binary Search Trees)
20:38
Back To Back SWE
Рет қаралды 360 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 901 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 460 М.
Subtree of Another Tree - Leetcode 572 - Python
14:15
NeetCode
Рет қаралды 175 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН