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; }
@VineetDixit2 ай бұрын
same brother
@ankushladani4968 ай бұрын
Kth largest ka logic pehle hi soch liya tha Bhaiya...❤ Thank You...✅🚀
@allinonemoviesyt8 ай бұрын
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-lp7wy8 ай бұрын
Nice explanation ASHU ❤
@allinonemoviesyt8 ай бұрын
@@AbhishekKumar-lp7wy thanks abhi
@ankushladani4968 ай бұрын
Array to BST pehle hi kar diya Kal video nahi aaya tha to khudse 5 problem solve kiye the... 💯✅
@rohitbisht11078 ай бұрын
Bhaiya web development ki bhi series laega apke padhane se sare doubt clear ho jate hai❤❤
@fachoyt96863 ай бұрын
Thank you so much for great explanation. My all doubts have been cleared after watching this Lecture.
@Chandan_agrawal5 ай бұрын
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-zl1xm8 ай бұрын
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.
@chanchalsoni75354 ай бұрын
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_pspk8 ай бұрын
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
@heetpatel30375 ай бұрын
Thankyou Ek dum badhiya content bhaiya thank you
@rishabhshenoy32583 ай бұрын
2:04 galat h because right side of left ko root wale se bhi compare karna hoga
@rishusachan26184 ай бұрын
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_Mukherjee8 ай бұрын
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-ku8ez7 ай бұрын
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); }
@beingbillionare20308 ай бұрын
thankyou bhaiya ethna mast content ke liye jo free diye hai aap
@asad_iliyas7 ай бұрын
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; }
@nepalihoni92135 ай бұрын
sc = log(n) / o(h), SC can never be o(n) in any worst case
@sumitvishwakarma10758 ай бұрын
Happy Mahashivratri Bhaiya ❤ Sab Chamak gaya ❤
@Pallab018 ай бұрын
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; }
@chanchalsoni75354 ай бұрын
this course is literally ....GOD LEVEL🥵.....unmatchable bruhh
@Pallab018 ай бұрын
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; }
@chanchalsoni75354 ай бұрын
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; }
@vivektechtuber2048 ай бұрын
Happy Shivratri Bhaiya Har Har Mahadev 🙏🙏
@CoderArmy98 ай бұрын
Happy Shivratri Bhai❣
@kartikay01115 ай бұрын
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
@relaxingtime24118 ай бұрын
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_387228 күн бұрын
To check the BST to checking the right > node and left < Node for each tree is not correct
@nandanks2068Ай бұрын
40:19 We can initialize prev=INT_MAX;
@dayashankarlakhotia49438 ай бұрын
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; } }
@vaishalichoudhary58 ай бұрын
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)
@sdtedit51658 ай бұрын
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(); }
@PankajBabu038 ай бұрын
GOOD MORNING BHAIYA ❤❤❤❤❤❤❤❤
@CoderArmy98 ай бұрын
Good Morning bhai❤
@amarnathtripathy4350Ай бұрын
array to bst question changed ?
@ankushladani4968 ай бұрын
Done Bhaiya... Day 165/180 ✅
@Pallab018 ай бұрын
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; }
@crazydude83338 ай бұрын
Bhaiya thought process kaise develop hoga DSA me bohot struggle hota hai??
@chaolobi76985 ай бұрын
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; }
@SayedHasinoor8 ай бұрын
Bhaiya BST to Double linkedlist & vice versa problem karo
@mohit62158 ай бұрын
Bhai aapki health kaise hai please reply🥰🥰
@dhirajpatil74538 ай бұрын
This course videos will be remain free forever or you will make it private because I have just started to watch this playlist sir..
@sumitvishwakarma10758 ай бұрын
bhai hamesha free rahega aap kabhi bhi dekh sakte ho
@vishalgupta-gi7kr8 ай бұрын
Sir please make video on web developer
@Aniruddha_Mukherjee8 ай бұрын
Good morning bhaiya ji😍
@CoderArmy98 ай бұрын
Good Morning bhai❤
@Harshmishra00078 ай бұрын
Ram ram bahi❤
@CoderArmy98 ай бұрын
Good Morning bhai❤
@ankushladani4968 ай бұрын
Thanks Bhaiya...❤🎉
@ankushladani4968 ай бұрын
Bhaiya 1st question ka code part khudse likha...😊
@YashSaini0078 ай бұрын
Bhaiya Radhe Radhe 🙏
@CoderArmy98 ай бұрын
Radhe Radhe bhai
@GamingArenaaa8 ай бұрын
printf("all clear"); printf("🔥🔥🔥🔥");
@allinonemoviesyt8 ай бұрын
use cout
@AMITKUMAR-ds4hp8 ай бұрын
Good morning boss ❤❤
@CoderArmy98 ай бұрын
Good Morning bhai❤
@Sanataniblood-mb5pu4 ай бұрын
Funny part is really funny bhaiya 😂😂
@Sanataniblood-mb5pu4 ай бұрын
Awesome content bhaiya ❤️
@shubhamkumarjha91928 ай бұрын
Good morning bhaiya ji💖, kaise hai tabiyat bhaiya ji aapki ab..?
@CoderArmy98 ай бұрын
Good Morning bhai❤, Ab sahi hai bhai
@anshyoutuber16088 ай бұрын
Bhaiya is Sunday live aayiye please
@Eng_wallah8 ай бұрын
Har har Mahadev 🙏🏻🙏🏻
@RajputArjun-ss4wj8 ай бұрын
Bhaiya java me bhi ek dsa course banaye
@deep_singh018 ай бұрын
Day 165/180 👍👍
@Karamjeet_118 ай бұрын
❤
@allinonemoviesyt8 ай бұрын
2:03 nhi bhaiya
@shrishtdev99618 ай бұрын
bhaiya ji namste❤❤❤
@YashSaini0078 ай бұрын
Day 164/180 #180DaysOfCode
@universalmeditationmusic92994 ай бұрын
nice
@gajendrasinghdhaked5 ай бұрын
🤩
@YASHYADAV-f9iАй бұрын
bhaiya maja a gaya
@itshirdeshk8 ай бұрын
Day 165 ✅🔥
@averagecoder78 ай бұрын
❤❤❤❤
@joychakraborty43677 ай бұрын
like done
@YashSaini0078 ай бұрын
Bhaiya ab aap thik ho.
@CoderArmy98 ай бұрын
Haan bhai❤
@akashkumawat-xk1eg2 ай бұрын
Logic galat h ki har node ko check kare left small and right bigger ho