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-lp7wy9 ай бұрын
Nice explanation ASHU ❤
@allinonemoviesyt9 ай бұрын
@@AbhishekKumar-lp7wy thanks abhi
@OK-ku8ez8 ай бұрын
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; }
@VineetDixit3 ай бұрын
same brother
@ankushladani4969 ай бұрын
Kth largest ka logic pehle hi soch liya tha Bhaiya...❤ Thank You...✅🚀
@beingbillionare20309 ай бұрын
thankyou bhaiya ethna mast content ke liye jo free diye hai aap
@ParvezKhan-mj7yt13 күн бұрын
Like kar do sab log. He needs to be motivated❤
@fachoyt96864 ай бұрын
Thank you so much for great explanation. My all doubts have been cleared after watching this Lecture.
@rohitbisht11079 ай бұрын
Bhaiya web development ki bhi series laega apke padhane se sare doubt clear ho jate hai❤❤
@heetpatel30376 ай бұрын
Thankyou Ek dum badhiya content bhaiya thank you
@sumitvishwakarma10759 ай бұрын
Happy Mahashivratri Bhaiya ❤ Sab Chamak gaya ❤
@ankushladani4969 ай бұрын
Array to BST pehle hi kar diya Kal video nahi aaya tha to khudse 5 problem solve kiye the... 💯✅
@vivektechtuber2049 ай бұрын
Happy Shivratri Bhaiya Har Har Mahadev 🙏🙏
@CoderArmy99 ай бұрын
Happy Shivratri Bhai❣
@RJFF3-r7w18 күн бұрын
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-ku8ez8 ай бұрын
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); }
@chanchalsoni75356 ай бұрын
this course is literally ....GOD LEVEL🥵.....unmatchable bruhh
@rishusachan26185 ай бұрын
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); } };
@PankajBabu039 ай бұрын
GOOD MORNING BHAIYA ❤❤❤❤❤❤❤❤
@CoderArmy99 ай бұрын
Good Morning bhai❤
@nikhil316_pspk9 ай бұрын
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_iliyas8 ай бұрын
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; }
@nepalihoni92136 ай бұрын
sc = log(n) / o(h), SC can never be o(n) in any worst case
@rishabhshenoy32584 ай бұрын
2:04 galat h because right side of left ko root wale se bhi compare karna hoga
@nandanks20683 ай бұрын
40:19 We can initialize prev=INT_MAX;
@chanchalsoni75356 ай бұрын
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-zl1xm9 ай бұрын
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.
@kartikay01116 ай бұрын
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
@Harshmishra00079 ай бұрын
Ram ram bahi❤
@CoderArmy99 ай бұрын
Good Morning bhai❤
@Sanataniblood-mb5pu5 ай бұрын
Funny part is really funny bhaiya 😂😂
@Aniruddha_Mukherjee9 ай бұрын
Good morning bhaiya ji😍
@CoderArmy99 ай бұрын
Good Morning bhai❤
@ankushladani4969 ай бұрын
Done Bhaiya... Day 165/180 ✅
@vaishalichoudhary59 ай бұрын
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)
@Pallab019 ай бұрын
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; }
@Pallab019 ай бұрын
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_agrawal6 ай бұрын
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.
@ankushladani4969 ай бұрын
Thanks Bhaiya...❤🎉
@AMITKUMAR-ds4hp9 ай бұрын
Good morning boss ❤❤
@CoderArmy99 ай бұрын
Good Morning bhai❤
@ParvezKhan-mj7yt12 күн бұрын
Check BST. Why create inorder fast. Can't we check directly through recursion
@YashSaini0079 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy99 ай бұрын
Radhe Radhe bhai
@relaxingtime24119 ай бұрын
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); }
@sdtedit51659 ай бұрын
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(); }
@mohit62159 ай бұрын
Bhai aapki health kaise hai please reply🥰🥰
@Aniruddha_Mukherjee9 ай бұрын
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; }
@chanchalsoni75356 ай бұрын
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-mb5pu5 ай бұрын
Awesome content bhaiya ❤️
@dayashankarlakhotia49439 ай бұрын
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; } }
@universalmeditationmusic92995 ай бұрын
nice
@vishalgupta-gi7kr9 ай бұрын
Sir please make video on web developer
@GamingArenaaa9 ай бұрын
printf("all clear"); printf("🔥🔥🔥🔥");
@allinonemoviesyt9 ай бұрын
use cout
@push_38722 ай бұрын
To check the BST to checking the right > node and left < Node for each tree is not correct
@deep_singh019 ай бұрын
Day 165/180 👍👍
@dhirajpatil74539 ай бұрын
This course videos will be remain free forever or you will make it private because I have just started to watch this playlist sir..
@sumitvishwakarma10759 ай бұрын
bhai hamesha free rahega aap kabhi bhi dekh sakte ho
@amarnathtripathy43502 ай бұрын
array to bst question changed ?
@ahmadhassan90590Ай бұрын
yes you can only do with tree method. now array method is not accepting
@crazydude83339 ай бұрын
Bhaiya thought process kaise develop hoga DSA me bohot struggle hota hai??
@shrishtdev99619 ай бұрын
bhaiya ji namste❤❤❤
@Pallab019 ай бұрын
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; }
@shubhamkumarjha91929 ай бұрын
Good morning bhaiya ji💖, kaise hai tabiyat bhaiya ji aapki ab..?
@CoderArmy99 ай бұрын
Good Morning bhai❤, Ab sahi hai bhai
@anshyoutuber16089 ай бұрын
Bhaiya is Sunday live aayiye please
@SayedHasinoor9 ай бұрын
Bhaiya BST to Double linkedlist & vice versa problem karo
@Eng_wallah9 ай бұрын
Har har Mahadev 🙏🏻🙏🏻
@RajputArjun-ss4wj9 ай бұрын
Bhaiya java me bhi ek dsa course banaye
@allinonemoviesyt9 ай бұрын
2:03 nhi bhaiya
@itshirdeshk9 ай бұрын
Day 165 ✅🔥
@ankushladani4969 ай бұрын
Bhaiya 1st question ka code part khudse likha...😊
@YashSaini0079 ай бұрын
Bhaiya ab aap thik ho.
@CoderArmy99 ай бұрын
Haan bhai❤
@YashSaini0079 ай бұрын
Day 164/180 #180DaysOfCode
@chaolobi76986 ай бұрын
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_119 ай бұрын
❤
@joychakraborty43678 ай бұрын
like done
@YASHYADAV-f9i2 ай бұрын
bhaiya maja a gaya
@averagecoder79 ай бұрын
❤❤❤❤
@RamgangakumarАй бұрын
bhaiya app kon se year m ho 😂😂😂😂
@gajendrasinghdhaked6 ай бұрын
🤩
@DelightfulTreeSwing-qo6tm9 ай бұрын
Nhi 2:00 logic shi nhi h
@akashkumawat-xk1eg3 ай бұрын
Logic galat h ki har node ko check kare left small and right bigger ho
@sabiruddinkhan4966 күн бұрын
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); } };