Binary Search Tree Interview Problem | 5 Binary Search Tree Problems in One Shot

  Рет қаралды 19,774

Coder Army

Coder Army

Күн бұрын

Пікірлер: 95
@CoderArmy9
@CoderArmy9 9 ай бұрын
Chamko do Binary Search Tree ko bhi....❤
@divyanshrawat2859
@divyanshrawat2859 5 ай бұрын
💙
@push_3872
@push_3872 2 ай бұрын
chamak gaya
@allinonemoviesyt
@allinonemoviesyt 9 ай бұрын
1:24:21 Homework - Time Complexity : O(N) hogi kyoki array ke har element ko ek baar traverse kiya hai Space Complexity : O(H) hogi H tree ki height hai , ye space hamara call stack lega Node* createBST(vector& nums, int s, int e){ // agar array me koi element hi na bacha ho to return krdo NULL if(s>e) return NULL; // array ka middle element find kro int mid = s+(e-s)/2; // jo middle element aaya hai usko root banao Node *root = new Node(nums[mid]); // array ke left part ko root ke left me daaldo root->left = createBST(nums,s,mid-1); // array ke right part ko root ke right me daaldo root->right = createBST(nums,mid+1,e); // phir root ko return krdo -- baaki ka kaam recursion khud krdega return root; }
@AbhishekKumar-lp7wy
@AbhishekKumar-lp7wy 9 ай бұрын
Nice explanation ASHU ❤
@allinonemoviesyt
@allinonemoviesyt 9 ай бұрын
@@AbhishekKumar-lp7wy thanks abhi
@OK-ku8ez
@OK-ku8ez 8 ай бұрын
Array to BST creation : TC = O(N) : Each node will be traversed SC = O(H) : log(N) Node* ArrayToBST(vector&nums, int start, int end) { if(start>end) return NULL; //Create Node int mid=(start+end)/2; Node*temp=new Node(nums[mid]); //Left Child temp->left=ArrayToBST(nums, start, mid-1); //Right Child temp->right=ArrayToBST(nums, mid+1, end); //Return address return temp; } void preorder(Node *root, vector&ans) { if(!root) return; ans.push_back(root->data); preorder(root->left, ans); preorder(root->right, ans); } vector sortedArrayToBST(vector& nums) { // Code here vectorans; Node *root=ArrayToBST(nums, 0, nums.size()-1); preorder(root, ans); return ans; }
@VineetDixit
@VineetDixit 3 ай бұрын
same brother
@ankushladani496
@ankushladani496 9 ай бұрын
Kth largest ka logic pehle hi soch liya tha Bhaiya...❤ Thank You...✅🚀
@beingbillionare2030
@beingbillionare2030 9 ай бұрын
thankyou bhaiya ethna mast content ke liye jo free diye hai aap
@ParvezKhan-mj7yt
@ParvezKhan-mj7yt 13 күн бұрын
Like kar do sab log. He needs to be motivated❤
@fachoyt9686
@fachoyt9686 4 ай бұрын
Thank you so much for great explanation. My all doubts have been cleared after watching this Lecture.
@rohitbisht1107
@rohitbisht1107 9 ай бұрын
Bhaiya web development ki bhi series laega apke padhane se sare doubt clear ho jate hai❤❤
@heetpatel3037
@heetpatel3037 6 ай бұрын
Thankyou Ek dum badhiya content bhaiya thank you
@sumitvishwakarma1075
@sumitvishwakarma1075 9 ай бұрын
Happy Mahashivratri Bhaiya ❤ Sab Chamak gaya ❤
@ankushladani496
@ankushladani496 9 ай бұрын
Array to BST pehle hi kar diya Kal video nahi aaya tha to khudse 5 problem solve kiye the... 💯✅
@vivektechtuber204
@vivektechtuber204 9 ай бұрын
Happy Shivratri Bhaiya Har Har Mahadev 🙏🙏
@CoderArmy9
@CoderArmy9 9 ай бұрын
Happy Shivratri Bhai❣
@RJFF3-r7w
@RJFF3-r7w 18 күн бұрын
Array to height balanced BST. T.comp = O(N),bcoz , all nodes will be created. S.comp=O(H) or we can say Log(N) due to recursive call. Node* sortedArrayToBST(vector& nums) { // Code here int n=nums.size(); Node *temp=create(nums,0,n-1); return temp; } Node *create(vector&nums,int start,int end) { if(start>end) { return NULL; } int mid=start+(end-start)/2; Node *temp=new Node(nums[mid]); temp->left=create(nums,start,mid-1); temp->right=create(nums,mid+1,end); return temp;
@OK-ku8ez
@OK-ku8ez 8 ай бұрын
Check BST : Upvote if you liked it bool BST(Node *root, int &prev) { if(!root) return true; return BST(root->left, prev) && (root->data > prev ? prev=root->data : 0) && BST(root->right, prev); } bool isBST(Node* root) { //Optimal approach(2) - My method int prev=INT_MIN; return BST(root, prev); }
@chanchalsoni7535
@chanchalsoni7535 6 ай бұрын
this course is literally ....GOD LEVEL🥵.....unmatchable bruhh
@rishusachan2618
@rishusachan2618 5 ай бұрын
Convert Sorted Array to Binary Search Tree class Solution { private: TreeNode* constructBST(const vector& nums, int left, int right) { if (left > right){ return NULL; } int mid = left + (right - left) / 2; TreeNode* node = new TreeNode(nums[mid]); node->left = constructBST(nums, left, mid - 1); node->right = constructBST(nums, mid + 1, right); return node; } public: TreeNode* sortedArrayToBST(vector& nums) { return constructBST(nums, 0, nums.size() - 1); } };
@PankajBabu03
@PankajBabu03 9 ай бұрын
GOOD MORNING BHAIYA ❤❤❤❤❤❤❤❤
@CoderArmy9
@CoderArmy9 9 ай бұрын
Good Morning bhai❤
@nikhil316_pspk
@nikhil316_pspk 9 ай бұрын
balanced BST ....... i think time complexity is O(n) and space complexity O(height of tree) bhai ek like kardo na #include using namespace std; #include class Node{ public: int data; Node *left,*right; Node(int val){ data=val; left=NULL; right=NULL; } }; Node* merge(vector&v,int start,int end){ if(start>end) return NULL; int mid=start+(end-start)/2; Node* temp=new Node(v[mid]); temp->left=merge(v,start,mid-1); temp->right=merge(v,mid+1,end); return temp; } void preorder(Node* root){ if(!root) return; cout
@asad_iliyas
@asad_iliyas 8 ай бұрын
H/W = Solution T.C. = O(N) creating each node by traversing each one of them. S.C = O(h) or O(N) recursive call will use stack memory and in worst case height of tree will be equal to N. class Node{ public: int data; Node *left,*right; Node(int value) { data = value; left = NULL; right = NULL; } }; class Solution { public: Node* BalancedBST(int start,int end,vector& arr) { if(start>end) return NULL; int mid = start + (end-start)/2; Node *temp = new Node(arr[mid]); temp->left = BalancedBST(start,mid-1,arr); temp->right = BalancedBST(mid+1,end,arr); return temp; } void preorder(Node *root,vector&ans) { if(!root) return; ans.push_back(root->data); preorder(root->left,ans); preorder(root->right,ans); } vector sortedArrayToBST(vector& nums) { // Code here Node *root = BalancedBST(0,nums.size()-1,nums); vectorans; preorder(root,ans); return ans; }
@nepalihoni9213
@nepalihoni9213 6 ай бұрын
sc = log(n) / o(h), SC can never be o(n) in any worst case
@rishabhshenoy3258
@rishabhshenoy3258 4 ай бұрын
2:04 galat h because right side of left ko root wale se bhi compare karna hoga
@nandanks2068
@nandanks2068 3 ай бұрын
40:19 We can initialize prev=INT_MAX;
@chanchalsoni7535
@chanchalsoni7535 6 ай бұрын
homework que ka ans TreeNode* solve(vector nums,int s,int e){ if(s>e)return NULL; int mid=s+(e-s)/2; TreeNode* temp=new TreeNode(nums[mid]); temp->left=solve(nums,s,mid-1); temp->right=solve(nums,mid+1,e); return temp; } TreeNode* sortedArrayToBST(vector& nums) { int n=nums.size(); int s=0; int e=n-1; TreeNode* ans= solve(nums,s,e); return ans; } };
@DarkConfidence-zl1xm
@DarkConfidence-zl1xm 9 ай бұрын
Hello bhiya i am doing bca from a random private collage i am in my 1st year but i have completed c and c++ completely and currently following your dsa series i am very glad to found a teacher link you i understand all the concept of dsa very easily thanku so much can you please tell me if i do hardwork so can i get good job after bca without doing mca.
@kartikay0111
@kartikay0111 6 ай бұрын
LeetCode 108 for Homework class Solution { public: void create(TreeNode* &root,vector &nums,int start,int end){ if(start>end) return; int mid= (start+end)/2; // TreeNode* node = new TreeNode(nums[mid]); root = new TreeNode(nums[mid]); create(root->left,nums,start,mid-1); create(root->right,nums,mid+1,end); } TreeNode* sortedArrayToBST(vector& nums) { TreeNode* root; create(root,nums,0,nums.size()-1); return root; } }; Time complexity = O(n) Space complexity = O(H) -->height of tree
@Harshmishra0007
@Harshmishra0007 9 ай бұрын
Ram ram bahi❤
@CoderArmy9
@CoderArmy9 9 ай бұрын
Good Morning bhai❤
@Sanataniblood-mb5pu
@Sanataniblood-mb5pu 5 ай бұрын
Funny part is really funny bhaiya 😂😂
@Aniruddha_Mukherjee
@Aniruddha_Mukherjee 9 ай бұрын
Good morning bhaiya ji😍
@CoderArmy9
@CoderArmy9 9 ай бұрын
Good Morning bhai❤
@ankushladani496
@ankushladani496 9 ай бұрын
Done Bhaiya... Day 165/180 ✅
@vaishalichoudhary5
@vaishalichoudhary5 9 ай бұрын
HOME WORK:) CREATE BST USING SORTED ARRAY: class Node{ public: int data; Node *left; Node *right; Node(int a){ data=a; left=NULL; right=NULL; } }; class Solution { public: Node* BST(vector&ans,int s,int e){ if(s>e){ return NULL; } int mid=s+(e-s)/2; Node *temp=new Node(ans[mid]); temp->left=BST(ans,s,mid-1); temp->right=BST(ans,mid+1,e); return temp; } vector sortedArrayToBST(vector& nums) { int s=0; int e=nums.size()-1; Node *head= BST(nums,s,e); } }; TIME COMPLEXITY:O(N) SPACE COMPLEXITY:O(N)
@Pallab01
@Pallab01 9 ай бұрын
5: Kth Smallest Element: void kSmallest(TreeNode *root,int &small,int &k) { //base case if(!root) return ; //LNR //left side kSmallest(root->left,small,k); if(k>0) { small=root->val; k--; } else return ; // right side kSmallest(root->right,small,k); } int Solution::kthsmallest(TreeNode* A, int B) { int small; kSmallest(A,small,B); return small; }
@Pallab01
@Pallab01 9 ай бұрын
3 .Sum of k smallest elements in BST void sumBst(Node*root,int &ans,int &k) { if(!root) return ; //left side sumBst(root->left,ans,k); //sum of k smallest element if(k!=0) { ans=ans+root->data; k=k-1; } else return ; //right side sumBst(root->right,ans,k); } int sum(Node* root, int k) { // Your code here int ans=0; sumBst(root,ans,k); return ans; }
@Chandan_agrawal
@Chandan_agrawal 6 ай бұрын
my logic: root node ke left child ke right most node and right child ke left most node ki value se bst check karenge in question no 1. but apka logic jyada simple hai find inorder traversal and go on.
@ankushladani496
@ankushladani496 9 ай бұрын
Thanks Bhaiya...❤🎉
@AMITKUMAR-ds4hp
@AMITKUMAR-ds4hp 9 ай бұрын
Good morning boss ❤❤
@CoderArmy9
@CoderArmy9 9 ай бұрын
Good Morning bhai❤
@ParvezKhan-mj7yt
@ParvezKhan-mj7yt 12 күн бұрын
Check BST. Why create inorder fast. Can't we check directly through recursion
@YashSaini007
@YashSaini007 9 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy9
@CoderArmy9 9 ай бұрын
Radhe Radhe bhai
@relaxingtime2411
@relaxingtime2411 9 ай бұрын
Problem 3 :- void inorder(Node *root, int &sum, int &k) { if(!root) return; inorder(root->left, sum, k); if(k>0){ sum+=root->data; k--; } inorder(root->right, sum, k); }
@sdtedit5165
@sdtedit5165 9 ай бұрын
kth largest using inorder and stack void elements(Node* root, stack&ans){ if(!root) return ; elements(root->left , ans); ans.push(root->data); elements(root->right, ans); } int kthLargest(Node *root, int K) { //Your code here stackans; elements(root, ans); int k2=K-1; while(k2--){ ans.pop(); } return ans.top(); }
@mohit6215
@mohit6215 9 ай бұрын
Bhai aapki health kaise hai please reply🥰🥰
@Aniruddha_Mukherjee
@Aniruddha_Mukherjee 9 ай бұрын
Problem 3 : void getSum(Node* root , int &k,int &sum) { if(root==NULL) return; //left most node tak ja rahe hain. kyunki hume chota elements left side me hi milta hain BST mein getSum(root->left, k , sum); /* ye if condition chalega jab left mein jate jate NULL mil jayega. aur ye bhi check kar rahe hain , ki kya hum K smallest elements dekh chuke hain ? agar k==0 ho jata hain to hum tree me ghumna bandh kar denge.*/ if(k>0) { sum+=root->data; k--; } //agar k==0 , matlab K smallest elements mil gaye toh return kar rahe hain. else { return; } // right subtree ki aur ja rahe hain. getSum(root->right,k,sum); } int sum(Node* root, int k) { // Your code here int sum = 0; getSum(root , k , sum); return sum; }
@chanchalsoni7535
@chanchalsoni7535 6 ай бұрын
ans of 3rd que void solve(Node* root,int &sum,int &k){ if(!root)return; solve(root->left,sum,k); if(k!=0){sum=sum+root->data; k=k-1; solve(root->right,sum,k); } } int sum(Node* root, int k) { // Your code here int sum=0; solve(root,sum,k); return sum; }
@Sanataniblood-mb5pu
@Sanataniblood-mb5pu 5 ай бұрын
Awesome content bhaiya ❤️
@dayashankarlakhotia4943
@dayashankarlakhotia4943 9 ай бұрын
class Solution { int ans=Integer.MAX_VALUE;prev=-1; public int minDiffInBST(TreeNode root){ if(root.left!=null) minDiffInBST(root.left); if(prev!=-1) ans=Math.min(ans,root.val-prev); prev=root.val; if(root.right!=null) minDiffInBST(root.right); return ans; } }
@universalmeditationmusic9299
@universalmeditationmusic9299 5 ай бұрын
nice
@vishalgupta-gi7kr
@vishalgupta-gi7kr 9 ай бұрын
Sir please make video on web developer
@GamingArenaaa
@GamingArenaaa 9 ай бұрын
printf("all clear"); printf("🔥🔥🔥🔥");
@allinonemoviesyt
@allinonemoviesyt 9 ай бұрын
use cout
@push_3872
@push_3872 2 ай бұрын
To check the BST to checking the right > node and left < Node for each tree is not correct
@deep_singh01
@deep_singh01 9 ай бұрын
Day 165/180 👍👍
@dhirajpatil7453
@dhirajpatil7453 9 ай бұрын
This course videos will be remain free forever or you will make it private because I have just started to watch this playlist sir..
@sumitvishwakarma1075
@sumitvishwakarma1075 9 ай бұрын
bhai hamesha free rahega aap kabhi bhi dekh sakte ho
@amarnathtripathy4350
@amarnathtripathy4350 2 ай бұрын
array to bst question changed ?
@ahmadhassan90590
@ahmadhassan90590 Ай бұрын
yes you can only do with tree method. now array method is not accepting
@crazydude8333
@crazydude8333 9 ай бұрын
Bhaiya thought process kaise develop hoga DSA me bohot struggle hota hai??
@shrishtdev9961
@shrishtdev9961 9 ай бұрын
bhaiya ji namste❤❤❤
@Pallab01
@Pallab01 9 ай бұрын
4: Kth largest element in BST void kLargest(Node *root,int &large,int &k) { //RNL if(!root) return ; // right side kLargest(root->right,large,k); if(k>0) { large=root->data; k--; } else return ; //left side kLargest(root->left,large,k); } int kthLargest(Node *root, int K) { //Your code here int large; kLargest(root,large,K); return large; }
@shubhamkumarjha9192
@shubhamkumarjha9192 9 ай бұрын
Good morning bhaiya ji💖, kaise hai tabiyat bhaiya ji aapki ab..?
@CoderArmy9
@CoderArmy9 9 ай бұрын
Good Morning bhai❤, Ab sahi hai bhai
@anshyoutuber1608
@anshyoutuber1608 9 ай бұрын
Bhaiya is Sunday live aayiye please
@SayedHasinoor
@SayedHasinoor 9 ай бұрын
Bhaiya BST to Double linkedlist & vice versa problem karo
@Eng_wallah
@Eng_wallah 9 ай бұрын
Har har Mahadev 🙏🏻🙏🏻
@RajputArjun-ss4wj
@RajputArjun-ss4wj 9 ай бұрын
Bhaiya java me bhi ek dsa course banaye
@allinonemoviesyt
@allinonemoviesyt 9 ай бұрын
2:03 nhi bhaiya
@itshirdeshk
@itshirdeshk 9 ай бұрын
Day 165 ✅🔥
@ankushladani496
@ankushladani496 9 ай бұрын
Bhaiya 1st question ka code part khudse likha...😊
@YashSaini007
@YashSaini007 9 ай бұрын
Bhaiya ab aap thik ho.
@CoderArmy9
@CoderArmy9 9 ай бұрын
Haan bhai❤
@YashSaini007
@YashSaini007 9 ай бұрын
Day 164/180 #180DaysOfCode
@chaolobi7698
@chaolobi7698 6 ай бұрын
GFG kth largest : - void inorder(Node *root, int &ans, int &k) { if (!root) { return; } if (k right, ans, k); if (k > 0) { ans = root->data; k--; } inorder(root->left, ans, k); } int kthLargest(Node *root, int K) { // Your code here int ans; inorder(root, ans, K); return ans; }
@Karamjeet_11
@Karamjeet_11 9 ай бұрын
@joychakraborty4367
@joychakraborty4367 8 ай бұрын
like done
@YASHYADAV-f9i
@YASHYADAV-f9i 2 ай бұрын
bhaiya maja a gaya
@averagecoder7
@averagecoder7 9 ай бұрын
❤❤❤❤
@Ramgangakumar
@Ramgangakumar Ай бұрын
bhaiya app kon se year m ho 😂😂😂😂
@gajendrasinghdhaked
@gajendrasinghdhaked 6 ай бұрын
🤩
@DelightfulTreeSwing-qo6tm
@DelightfulTreeSwing-qo6tm 9 ай бұрын
Nhi 2:00 logic shi nhi h
@akashkumawat-xk1eg
@akashkumawat-xk1eg 3 ай бұрын
Logic galat h ki har node ko check kare left small and right bigger ho
@sabiruddinkhan496
@sabiruddinkhan496 6 күн бұрын
T.C:O(N); S.C:O(H); H->height of the BST; class Solution { public: Node*converttobst(vector&nums,Node*root,int s,int e){ if(s>e) return NULL; int mid=s+(e-s)/2; root=new Node(nums[mid]); root->left=converttobst(nums,root->left,s,mid-1); root->right=converttobst(nums,root->right,mid+1,e); return root; } Node* sortedArrayToBST(vector& nums) { Node*root=NULL; return converttobst(nums,root,0,nums.size()-1); } };
@DelightfulTreeSwing-qo6tm
@DelightfulTreeSwing-qo6tm 9 ай бұрын
Nhi 2:00 logic shi nhi h
@utkarshrathore2990
@utkarshrathore2990 9 ай бұрын
😍😍
Binary Search Tree Problems for Coding Round
1:28:53
Coder Army
Рет қаралды 10 М.
L52. Recover BST | Correct BST with two nodes swapped
15:56
take U forward
Рет қаралды 137 М.
Хаги Ваги говорит разными голосами
0:22
Фани Хани
Рет қаралды 2,2 МЛН
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
Arpit Bhayani talks about real engineering for 1 hour straight
1:16:23
AVL Trees & Rotations (Self-Balancing Binary Search Trees)
20:38
Back To Back SWE
Рет қаралды 360 М.
L44. Delete a Node in Binary Search Tree | BST | C++ | Java
15:48
take U forward
Рет қаралды 219 М.
Хаги Ваги говорит разными голосами
0:22
Фани Хани
Рет қаралды 2,2 МЛН