Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@rishabsharma53073 жыл бұрын
Waiting for Morris Inorder Traversal
@takeUforward3 жыл бұрын
@@rishabsharma5307 That will be the best video of this series.
@rishabsharma53073 жыл бұрын
@@takeUforward can't wait for it 🤩
@rohan8758 Жыл бұрын
For Left Side View , call the recursive function with the left node and then with the right node . f(node->left, level+1); f(node->right, level+1);
@kuldeepkushwah5653 жыл бұрын
Thank you striver for all your effort, your content is worth more than any paid courses. This Chanel is full of treasure.
@sarathchandra9412 жыл бұрын
Hiidden behind the youtube recommendation algo..🥺 #open sesame😎 -- tree ka free series treasure will be urs🤭
@krishnasudan34103 жыл бұрын
My iterative approach.. thanks Bhaiya for everything.. Such a gem in coding community vector rightView(Node *root){ vector res; if(root==NULL) return res; queue q; q.push(root); while(!q.empty()){ Node *temp = q.front(); res.push_back(temp->data); int size = q.size(); for(int i=0;iright!=NULL) q.push(curr->right); if(curr->left!=NULL) q.push(curr->left); } } return res; }
@gautamjh2 жыл бұрын
In your code, you should push left node before the right node in the queue in for loop
@isheep9025 Жыл бұрын
@@gautamjh no its correct for right view
@rutwikmore7462 Жыл бұрын
bro I also wrote the exact same code..😄😄
@uRamPlus2 жыл бұрын
self notes: 🚀 for every level, the first node (on the right side) will be our right side view 🚀 if the level of the tree == my vector's size, I need to push it into my vector 🚀 if at any point we reach a null node, we just need to return (base case)
@moksh4557 ай бұрын
That code is self explanatory no need to maintain notes
@ritwikdurga38554 ай бұрын
@@moksh455go touch grass
@anmolswarnkar77073 жыл бұрын
Amazing explanation! I like the fact that you go step by step (like a compiler would) over the example. Keep posting these videos!
@VishalGupta-xw2rp2 жыл бұрын
What i came up was that (before watching video) For(Right View) Use Vertical order only This time instead of left - 1, we will increase both and Mark each node *levels* not their *vertical* left + 1, right + 1 Now simply as we go we will store the latest level by m[level] = node->data. Because of map property, it will itself ensure that all the latest one will be there so overwrites the previous But what a technique and Amazing approach.... Man when will i begin to think like that ♥️🔥
@priyankarkoley6 ай бұрын
I believe that will end up with nlog(n) time complexity.
@blitzkrieg54543 жыл бұрын
I feel so lucky to have a teacher like you, thanks a lottt!!!
@shubh137992 жыл бұрын
your explanation techniques are phenomenal, clear concepts and concise code. Loving the way you code.
@Dontpushyour_luck Жыл бұрын
so clever technique of using recursion and size of data structure to check if it is the first node that we came to in this level. no wonder you are candidate master on codeforces!
@samarthsingh2705 Жыл бұрын
I first watched the video of apna college youtube channel where they discussed the iterative method using level order traversal. This code here in this video is very short and also very well explained by striver. Thanks TUF for such amazing content.
@4mulate3 ай бұрын
My iterative approach which was before I watched your explanation: //just push to ans vector when we are at last node in the queue. class Solution { public: vector rightSideView(TreeNode* root) { vector ans; if(root==nullptr) return {}; queue q; q.push(root); while(!q.empty()){ int size = q.size(); for(int i=0;ival); } q.pop(); if(front->left!=nullptr) q.push(front->left); if(front->right!=nullptr) q.push(front->right); } } return ans; } };
@animeshbarole Жыл бұрын
if(level==ds.size()) ans.push_back(node->val) is just Mind blowing technique ....... Take a bow Striver .
@falgunitagadkar4097 Жыл бұрын
Your approach to solve problems is just commendable👏👏 Thanks for providing so valuable content for free! Again...the same thing...Hats off to you Striver ✌
@HimanshuSingh-hd8of3 жыл бұрын
//Implemetation of above approach Initially we pass 0 in level res is data structure that we are using for storing the elements void find_right_view(Node *root,vector&res,int level){ if(root==NULL){return ;} if(level==res.size()){ res.push_back(root->data); } find_right_view(root->right,res,level+1); find_right_view(root->left,res,level+1); }
@ojasthengadi96812 жыл бұрын
hey can you please explain why the level becomes 0 once we reach to root node before travsersing to left
@yogeshjoshi78114 күн бұрын
I saw the video of top view, then I solved botom view on my own then I just changed line-1 to line+1 in that bottom view solution and I got this answer right. just a single change thank you striver
@ArushiRoy28236 ай бұрын
Oaw , what an amazing explanation and logic behind the code is superb 🥺
@divyansh22122 жыл бұрын
Easy solution for left view level order traversal: #include using namespace std; vector getLeftView(TreeNode *root) { if (root == NULL) return {}; vector ans; queue q; q.push(root); while (!q.empty()) { int sz = q.size(); for (int i = 0; i < sz; i++) { auto front = q.front(); q.pop(); if (i == 0) ans.push_back(front->data); if (front->left) q.push(front->left); if (front->right) q.push(front->right); } } return ans; }
@rajnishism2898 Жыл бұрын
For Left Side View , call the recurssive function with left node and then with the right node . f(node->left, level+1); f(node->right, level+1);
@rishabhinc293610 ай бұрын
i could have never ever thought of comparing level and ds size.striver ur insane man
there should right first in the for loop for rightside view rest all is correct 🙂
@avicr47272 жыл бұрын
@@dronrahangdale2361 he put i= size-1 if i=0 then right should come first
@ScienceSeekho2 жыл бұрын
// BFS - Iterative - keeping a last variable. TC: O(N) SC: O(N) // -- In worst case all bottom nodes will be stored in queue, to avoid this we go with DFS vector rightSideView(TreeNode* root) { vector ans; if(root==NULL) return ans; queue q; q.push(root); while(!q.empty()) { int n = q.size(); int last; while(n--) { TreeNode* tn = q.front(); q.pop(); if(tn->left) q.push(tn->left); if(tn->right) q.push(tn->right); last = tn->val; } ans.push_back(last); } return ans; }
@rohandevaki43492 жыл бұрын
very simple approach and great explaination, how do you get these approaches? i wasnt able to get any approach even after trying so much.
@dheepakraaj83523 жыл бұрын
Understood bhaiya thank you. Level Order Traversal Apporach: During level order traversal ,we had one size variable which tells the size of queue at each level so if i==size-1; then that element is the last element of that level so we can push it into the data structure. Is this approach right bhaiya?
@takeUforward3 жыл бұрын
Yeah.. correct.
@dheepakraaj83523 жыл бұрын
@@takeUforward thank you bhaiya 💖😃
@PalakMittal3 жыл бұрын
In case anyone want iterative method 😅 class Solution { public: //Function to return list containing elements of right view of binary tree. vector rightView(Node *root) { // Your Code here vectorres; if(!root) return res; queueq; q.push(root); while(!q.empty()){ int n=q.size(); for(int i=0;idata); } Node* node=q.front(); q.pop(); if(node->right) q.push(node->right); if(node->left) q.push(node->left); } } return res; } }; PS: Recursive one was just mind blowing 😍😍
@Sujit134843 жыл бұрын
Good work. I like the way you explain each problem. Once change we can do. I think we can do it without using Stack also by keeping a counter for max level reached from right tree. Here is code below. class Solution { int maxLevelSoFar = -1; public List rightSideView(TreeNode root) { List result = new ArrayList(); rightRecursive(root, 0, result); return result; } private void rightRecursive(TreeNode root, int level, List result) { if(root == null) { return; } if(level > maxLevelSoFar) { result.add(root.val); } rightRecursive(root.right, level + 1, result); rightRecursive(root.left, level + 1, result); maxLevelSoFar = Math.max(maxLevelSoFar, level); } }
@maradanikhil6882 Жыл бұрын
Here is the level order traversal for right side view: class Solution { public: vector rightSideView(TreeNode* root) { queueq; q.push(root); vectorans; if(root==NULL) return ans; while(!q.empty()){ int size=q.size(); int a=0; for(int i=0;ival; if(node->left) q.push(node->left); if(node->right) q.push(node->right); } ans.push_back(a); }return ans; } };
@iamnottech89183 ай бұрын
I solved this on my own using horizontal line , I was thinking that recursion will work on this came here to conform .. thanks...
@ssaha77142 жыл бұрын
Too good. Amazed to see the explanation… clear and to the point..
@pranav49692 жыл бұрын
Even if the tree is perfectly balanced for level order, the maximum number of nodes at any level will always be less than log(N) - 1. So level order takes almost same space as recursive for balanced trees. And also level order takes less space for skewed trees. So space wise , level order is better than recursive
@tear7934 Жыл бұрын
how is it log(N) - 1 can you please explain
@prabhakaran5542Ай бұрын
Understood ❤
@apmotivationakashparmar7224 күн бұрын
Thank you so much Striver ! .
@BTEEELavkushYadav3 жыл бұрын
Millions of thanks to you bro you make my concept crystal clear I have subscribed ur channel instant
@NavyasriThalluri8 ай бұрын
The way how you use recursion is damn good
@ishangujarathi10 Жыл бұрын
awesome logic of level==ds.size(), unserstood in depth !!!
@anishamajumder19405 ай бұрын
for left side view - use preorder traversal and get the first element
@maneetrajgupta Жыл бұрын
correction... iterative way's space complexity can be optimized to O(H).
@stormshadow76 Жыл бұрын
This is the most brilliant solution !!!!!!! Amazing !!!
@sujan_kumar_mitra3 жыл бұрын
Understood! Nice intuition to figure out if we are visiting the depth first or not
@per.seus._11 ай бұрын
UNDERSTOOD
@prajaktakapoor75209 ай бұрын
Great solution sir!! Really impressed
@SohamDutta2224 ай бұрын
Man, You are the best.. best explanation
@sharmanihal994 ай бұрын
This DFS Solution was more intutive for me. class Solution: def __init__(self): self.rightmost_values = defaultdict(int) def rightSideView(self, root: Optional[TreeNode]) -> List[int]: if not root: return [] self.reversePreorder(root, 0) return self.rightmost_values.values() def reversePreorder(self, root, level): if not root: return if level not in self.rightmost_values: self.rightmost_values[level] = root.val # Traverse right subtree first for getting rightmost values self.reversePreorder(root.right, level + 1) self.reversePreorder(root.left, level + 1)
@sifatsamir00763 ай бұрын
nice explaination ❤ Love from Bangladesh❤
@cinime2 жыл бұрын
Understood! So amazing explanation as always, thank you very much!!
@areebahmadsiddiqui26483 жыл бұрын
I am watching your channel the very first time and maaaaan you are damn good thanq for this
@pranalipardeshi77483 жыл бұрын
for 11:49 we can keep same pre order traversal i.e first left and then right
@guneetgupta3356 Жыл бұрын
for left view tree, you should go for root left right raversal
@sachinvarma99493 ай бұрын
that was a mind blown solution
@mohit77174 ай бұрын
for left view :- Root Left Right ... I think inorder will work .. I just comment after you told to comment down how to do left view....
@tanaykamath14152 жыл бұрын
I managed to solve this using an iterative approach but the soln. was complex af!, how do you manage to come up with such easy solns. 🙈🙈
@U-DAY2 жыл бұрын
bro i never saw this much like amazing explanation ..... superb awesome u are.... loving ur teaching bro........................
@lifehustlers164 Жыл бұрын
Completed 25/54(46% done) !!!
@avinashgupta23082 жыл бұрын
Amazing approach! and definitely the way you teach is just ❤
Amazing Striver.. Such an amazing explanation. I used BFS and it was not as clean as your code.
@nitunsingh69863 жыл бұрын
We can use set data structure over here to store right view only same for left view.
@shreyasvishwakarma89793 жыл бұрын
11:39 just exchange f ( node -> right ) and f ( left->left )
@golangNinja296 ай бұрын
We could have used , map instead of array, in which if key( level) already exists then ignore And iteration will be same as shown in video
@krishnaradhey2814 Жыл бұрын
On GFG if you traverse via QUEUE it gives segmentation fault.... CODE:- /* A binary tree node struct Node { int data; struct Node* left; struct Node* right; Node(int x){ data = x; left = right = NULL; } }; */ //Function to return a list containing elements of left view of the binary tree. vector leftView(Node *root) { // Your code here vectorans; vectorvec; queuequ; qu.push(root); while(qu.empty() != true) { int size = qu.size(); vectordemo; while(size--) { Node* temp = qu.front(); qu.pop(); demo.push_back(temp->data); if(temp->left != nullptr)qu.push(temp->left); if(temp->right != nullptr)qu.push(temp->right); } vec.push_back(demo); } for(int i = 0 ;i
@lakshsinghania Жыл бұрын
Iterative sol for the right side view of the binary tree class Solution { public: vector rightSideView(TreeNode* root) { vector ans; queue q; q.push(root); if(root == NULL) return ans; while(!q.empty()){ TreeNode* node = q.front(); ans.push_back(node -> val); int s = q.size(); for(int i =0; i right != NULL) q.push(node -> right); if(node -> left != NULL) q.push(node -> left); } } return ans; } };
@jonu.1504 Жыл бұрын
Recursive is easy than Iterative. Which one is good performance wise?
@divyan154 Жыл бұрын
instead of reverse preorder traversal do traditional preorder and rest same
@DeadPoolx171223 сағат бұрын
UNDERSTOOD;
@raghavkhandelwal10943 жыл бұрын
for left view Instead of Reverse pre order trav. i.e, RRL take preorder trav. as the approach i.e., RLR
@takeUforward3 жыл бұрын
yes correct, amazing!
@harshopes9 ай бұрын
Note to self: left side view call a recursve function with the root,list,level leftViewHelper(root.left, list, level+1); leftViewHelper(root.right, list, level+1); visaVersa for right side view
@nikhilnagrale3 жыл бұрын
// Level Order Traversal Variation // Time Complexity - O(N) // Space Complexity - O(N) class Solution { public: vector rightSideView(TreeNode* root) { vector res; if (!root) return {}; queue q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { TreeNode* curr = q.front(); q.pop(); if (i == 0) res.push_back(curr->val); if (curr->right) q.push(curr->right); if (curr->left) q.push(curr->left); } } return res; } };
@adityajain12052 жыл бұрын
This code is for left view
@nikhilnagrale2 жыл бұрын
@@adityajain1205 bruh if you think that way, then you didn't understand the concept. Watch video again
@ArvindYadav-gy7fj2 жыл бұрын
@@adityajain1205 i think it is for right view
@VivekSharma-eh2tv4 ай бұрын
for the left view we, will call the recursive function for the left node and then the right node -> the logic of returning remains same but the direction of movement alters .. am i right @takeUforward
@Rajat_maurya2 жыл бұрын
i bow my head... bhagwan apko sari khushiya de app itta bada kam free m kar rahe
For Left View C++ Code : void left(Node *root,vector&m,int l){ if(root==NULL) return ; if(m.size()==l) { m.push_back(root->data); } left(root->left,m,l+1); left(root->right,m,l+1); } vector leftView(Node *root) { // Your code here vectorv; int level=0; left(root,v,level); return v; }
@devanshmesson27772 жыл бұрын
I think the space complexity for recursive approach will be O(N), because we end up traversing the whole tree, So, there will be N recursive calls.
@yashtarwe6878 Жыл бұрын
very good explanation and approach
@urdaddy8520 Жыл бұрын
Initially I was thinking to use level order traversal using deque, and then store last value of deque in our answer; can it be correct?? I'm confused...
@varunvishwakarma96892 жыл бұрын
In-Recursive way: if we take Complete Binary Tree (No-skewed) then also Auxilary space will be O(N) only na?......and not O(h) because its traversing to every node and for each node stack space is alloted in backend
@stark35853 жыл бұрын
Vey good concept of vertical lines. Keep going bro....
@bollamajay9794 Жыл бұрын
python code: from collections import deque def rightView(self,root): if root is None: return [ ] q=deque() q.append(root) res=[] while q: count=len(q) for i in range(count): node=q.popleft() if i==(count-1): res.append(node.data) if node.left is not None: q.append(node.left) if node.right is not None: q.append(node.right) return res
@ManishYadav-dq9xm Жыл бұрын
shouldn't the level count also increase if it returns to left side after checking that right is null?
@bhalulal5947 Жыл бұрын
Same I have doubt?? Does anyone has solution for this??
@ssplusultra86458 ай бұрын
why arent u doing morris traversal? space would be O(1)
@Kaushik846 Жыл бұрын
Awesome technique and explanation!!!
@Prateek032 жыл бұрын
Thank you striver, you made things easy for us 🔥
@ayushkushwaha171 Жыл бұрын
we can make use of map to solve this as well just like your previous top & bottom view questions
@abhishekkumarjhariya13403 жыл бұрын
for the left view we will simply take the normal preorder (root, left, right) and rest of the things will remain same.
@gourangpathak44432 жыл бұрын
Yeah I also think the same
@omkarshendge54382 жыл бұрын
yeah same i also thought of this!
@sourabh2582 жыл бұрын
Excellent stuff, thanks for such a simple explanation!
@captainakash89143 ай бұрын
for left view just change the traversal order
@bollamajay9794 Жыл бұрын
python code: def LeftView(root): if root is None: return [ ] q=deque() q.append(root) res=[] while q: count=len(q) for i in range(count): node=q.popleft() if i==0: res.append(node.data) if node.left is not None: q.append(node.left) if node.right is not None: q.append(node.right) return res
@hanish6713Ай бұрын
whats time complexity of the code ans also space complexity?
@shaiksoofi37413 ай бұрын
understod
@SohamPatil-k9z26 күн бұрын
The second approach will acutally not work for in some cases . for example take the same binary tree in video and suppose 7 has a one left child 19 and 19 has one more left child 20,and also suppose 5 has one right child 21 and 21 has one right child 22.So in this case the 20 will be rightside view for level 5 which is wrong because 22 will be the rightmost for level 5 . Correct me i am wrong somewhere .
@tejaswaniguttula59615 ай бұрын
This is what I came up for left view using level order traversal:- vector leftView(Node* root) { vector ans; if(root == nullptr) return ans; map mpp; queue q; q.push({root, 0}); while(!q.empty()){ auto p = q.front(); q.pop(); Node* node = p.first; int line = p.second; mpp[line] = node -> data; if(node -> right){ q.push({node -> right, line + 1 }); } if(node -> left){ q.push({node -> left, line+1}); } } for(auto it: mpp ){ ans.push_back(it.second); } return ans; }
@tejaswaniguttula59615 ай бұрын
For right view using level order traversal :- vector rightSideView(TreeNode* root) { vector ans; if(root == nullptr) return ans; map mpp; queue q; q.push({root, 0}); while(!q.empty()){ auto p = q.front(); q.pop(); TreeNode* node = p.first; int line = p.second; mpp[line] = node -> val; if(node -> left){ q.push({node -> left, line+1}); } if(node -> right){ q.push({node -> right, line + 1 }); } } for(auto it: mpp ){ ans.push_back(it.second); } return ans; }
@sonakshibajpai64452 ай бұрын
understood
@sahilsaharn59632 жыл бұрын
So we have to keep track of max level if it filled we skip the other node and if not we include it
@akhilakapse2042 жыл бұрын
Please keep at it bro. These videos are super helpful!