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

  Рет қаралды 18,162

Coder Army

Coder Army

Күн бұрын

Пікірлер
@CoderArmy9
@CoderArmy9 8 ай бұрын
Chamko do Binary Search Tree ko bhi....❤
@divyanshrawat2859
@divyanshrawat2859 4 ай бұрын
💙
@push_3872
@push_3872 28 күн бұрын
chamak gaya
@OK-ku8ez
@OK-ku8ez 7 ай бұрын
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 2 ай бұрын
same brother
@ankushladani496
@ankushladani496 8 ай бұрын
Kth largest ka logic pehle hi soch liya tha Bhaiya...❤ Thank You...✅🚀
@allinonemoviesyt
@allinonemoviesyt 8 ай бұрын
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 8 ай бұрын
Nice explanation ASHU ❤
@allinonemoviesyt
@allinonemoviesyt 8 ай бұрын
@@AbhishekKumar-lp7wy thanks abhi
@ankushladani496
@ankushladani496 8 ай бұрын
Array to BST pehle hi kar diya Kal video nahi aaya tha to khudse 5 problem solve kiye the... 💯✅
@rohitbisht1107
@rohitbisht1107 8 ай бұрын
Bhaiya web development ki bhi series laega apke padhane se sare doubt clear ho jate hai❤❤
@fachoyt9686
@fachoyt9686 3 ай бұрын
Thank you so much for great explanation. My all doubts have been cleared after watching this Lecture.
@Chandan_agrawal
@Chandan_agrawal 5 ай бұрын
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.
@DarkConfidence-zl1xm
@DarkConfidence-zl1xm 8 ай бұрын
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.
@chanchalsoni7535
@chanchalsoni7535 4 ай бұрын
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; } };
@nikhil316_pspk
@nikhil316_pspk 8 ай бұрын
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
@heetpatel3037
@heetpatel3037 5 ай бұрын
Thankyou Ek dum badhiya content bhaiya thank you
@rishabhshenoy3258
@rishabhshenoy3258 3 ай бұрын
2:04 galat h because right side of left ko root wale se bhi compare karna hoga
@rishusachan2618
@rishusachan2618 4 ай бұрын
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); } };
@Aniruddha_Mukherjee
@Aniruddha_Mukherjee 8 ай бұрын
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; }
@OK-ku8ez
@OK-ku8ez 7 ай бұрын
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); }
@beingbillionare2030
@beingbillionare2030 8 ай бұрын
thankyou bhaiya ethna mast content ke liye jo free diye hai aap
@asad_iliyas
@asad_iliyas 7 ай бұрын
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 5 ай бұрын
sc = log(n) / o(h), SC can never be o(n) in any worst case
@sumitvishwakarma1075
@sumitvishwakarma1075 8 ай бұрын
Happy Mahashivratri Bhaiya ❤ Sab Chamak gaya ❤
@Pallab01
@Pallab01 8 ай бұрын
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; }
@chanchalsoni7535
@chanchalsoni7535 4 ай бұрын
this course is literally ....GOD LEVEL🥵.....unmatchable bruhh
@Pallab01
@Pallab01 8 ай бұрын
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; }
@chanchalsoni7535
@chanchalsoni7535 4 ай бұрын
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; }
@vivektechtuber204
@vivektechtuber204 8 ай бұрын
Happy Shivratri Bhaiya Har Har Mahadev 🙏🙏
@CoderArmy9
@CoderArmy9 8 ай бұрын
Happy Shivratri Bhai❣
@kartikay0111
@kartikay0111 5 ай бұрын
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
@relaxingtime2411
@relaxingtime2411 8 ай бұрын
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); }
@push_3872
@push_3872 28 күн бұрын
To check the BST to checking the right > node and left < Node for each tree is not correct
@nandanks2068
@nandanks2068 Ай бұрын
40:19 We can initialize prev=INT_MAX;
@dayashankarlakhotia4943
@dayashankarlakhotia4943 8 ай бұрын
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; } }
@vaishalichoudhary5
@vaishalichoudhary5 8 ай бұрын
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)
@sdtedit5165
@sdtedit5165 8 ай бұрын
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(); }
@PankajBabu03
@PankajBabu03 8 ай бұрын
GOOD MORNING BHAIYA ❤❤❤❤❤❤❤❤
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good Morning bhai❤
@amarnathtripathy4350
@amarnathtripathy4350 Ай бұрын
array to bst question changed ?
@ankushladani496
@ankushladani496 8 ай бұрын
Done Bhaiya... Day 165/180 ✅
@Pallab01
@Pallab01 8 ай бұрын
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; }
@crazydude8333
@crazydude8333 8 ай бұрын
Bhaiya thought process kaise develop hoga DSA me bohot struggle hota hai??
@chaolobi7698
@chaolobi7698 5 ай бұрын
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; }
@SayedHasinoor
@SayedHasinoor 8 ай бұрын
Bhaiya BST to Double linkedlist & vice versa problem karo
@mohit6215
@mohit6215 8 ай бұрын
Bhai aapki health kaise hai please reply🥰🥰
@dhirajpatil7453
@dhirajpatil7453 8 ай бұрын
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 8 ай бұрын
bhai hamesha free rahega aap kabhi bhi dekh sakte ho
@vishalgupta-gi7kr
@vishalgupta-gi7kr 8 ай бұрын
Sir please make video on web developer
@Aniruddha_Mukherjee
@Aniruddha_Mukherjee 8 ай бұрын
Good morning bhaiya ji😍
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good Morning bhai❤
@Harshmishra0007
@Harshmishra0007 8 ай бұрын
Ram ram bahi❤
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good Morning bhai❤
@ankushladani496
@ankushladani496 8 ай бұрын
Thanks Bhaiya...❤🎉
@ankushladani496
@ankushladani496 8 ай бұрын
Bhaiya 1st question ka code part khudse likha...😊
@YashSaini007
@YashSaini007 8 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy9
@CoderArmy9 8 ай бұрын
Radhe Radhe bhai
@GamingArenaaa
@GamingArenaaa 8 ай бұрын
printf("all clear"); printf("🔥🔥🔥🔥");
@allinonemoviesyt
@allinonemoviesyt 8 ай бұрын
use cout
@AMITKUMAR-ds4hp
@AMITKUMAR-ds4hp 8 ай бұрын
Good morning boss ❤❤
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good Morning bhai❤
@Sanataniblood-mb5pu
@Sanataniblood-mb5pu 4 ай бұрын
Funny part is really funny bhaiya 😂😂
@Sanataniblood-mb5pu
@Sanataniblood-mb5pu 4 ай бұрын
Awesome content bhaiya ❤️
@shubhamkumarjha9192
@shubhamkumarjha9192 8 ай бұрын
Good morning bhaiya ji💖, kaise hai tabiyat bhaiya ji aapki ab..?
@CoderArmy9
@CoderArmy9 8 ай бұрын
Good Morning bhai❤, Ab sahi hai bhai
@anshyoutuber1608
@anshyoutuber1608 8 ай бұрын
Bhaiya is Sunday live aayiye please
@Eng_wallah
@Eng_wallah 8 ай бұрын
Har har Mahadev 🙏🏻🙏🏻
@RajputArjun-ss4wj
@RajputArjun-ss4wj 8 ай бұрын
Bhaiya java me bhi ek dsa course banaye
@deep_singh01
@deep_singh01 8 ай бұрын
Day 165/180 👍👍
@Karamjeet_11
@Karamjeet_11 8 ай бұрын
@allinonemoviesyt
@allinonemoviesyt 8 ай бұрын
2:03 nhi bhaiya
@shrishtdev9961
@shrishtdev9961 8 ай бұрын
bhaiya ji namste❤❤❤
@YashSaini007
@YashSaini007 8 ай бұрын
Day 164/180 #180DaysOfCode
@universalmeditationmusic9299
@universalmeditationmusic9299 4 ай бұрын
nice
@gajendrasinghdhaked
@gajendrasinghdhaked 5 ай бұрын
🤩
@YASHYADAV-f9i
@YASHYADAV-f9i Ай бұрын
bhaiya maja a gaya
@itshirdeshk
@itshirdeshk 8 ай бұрын
Day 165 ✅🔥
@averagecoder7
@averagecoder7 8 ай бұрын
❤❤❤❤
@joychakraborty4367
@joychakraborty4367 7 ай бұрын
like done
@YashSaini007
@YashSaini007 8 ай бұрын
Bhaiya ab aap thik ho.
@CoderArmy9
@CoderArmy9 8 ай бұрын
Haan bhai❤
@akashkumawat-xk1eg
@akashkumawat-xk1eg 2 ай бұрын
Logic galat h ki har node ko check kare left small and right bigger ho
@Ramgangakumar
@Ramgangakumar 3 күн бұрын
bhaiya app kon se year m ho 😂😂😂😂
@DelightfulTreeSwing-qo6tm
@DelightfulTreeSwing-qo6tm 8 ай бұрын
Nhi 2:00 logic shi nhi h
@DelightfulTreeSwing-qo6tm
@DelightfulTreeSwing-qo6tm 8 ай бұрын
Nhi 2:00 logic shi nhi h
@utkarshrathore2990
@utkarshrathore2990 8 ай бұрын
😍😍
Binary Search Tree Problems for Coding Round
1:28:53
Coder Army
Рет қаралды 9 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 11 МЛН
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 8 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 31 МЛН
How I Tricked My Brain to Be Addicted to Coding (The Dopamine Hack)
8:06
Ashish Pratap Singh
Рет қаралды 100 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
How I Mastered Data Structures and Algorithms in 8 Weeks
15:46
Aman Manazir
Рет қаралды 94 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 246 М.
10.1 AVL Tree - Insertion and Rotations
43:08
Abdul Bari
Рет қаралды 1,2 МЛН
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 1,8 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 11 МЛН