L24. Right/Left View of Binary Tree | C++ | Java

preview_player
Показать описание


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

Checkout the problem link 👇🏼
Right/Left View of Binary Tree | C++ | Java

Рекомендации по теме
Комментарии
Автор

Hi, hope you are well.


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL and many other time saving features under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing yourself.


takeUforward
Автор

I feel so lucky to have a teacher like you, thanks a lottt!!!

thunder-storm-power
Автор

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)

uRamPlus
Автор

Thank you striver for all your effort, your content is worth more than any paid courses.
This Chanel is full of treasure.

kuldeepkushwah
Автор

Amazing explanation! I like the fact that you go step by step (like a compiler would) over the example. Keep posting these videos!

anmolswarnkar
Автор

if(level==ds.size()) ans.push_back(node->val) is just Mind blowing technique Take a bow Striver .

animeshbarole
Автор

Please likeeee, shareeee and :) Also follow me at Insta: Striver_79

takeUforward
Автор

My iterative approach.. thanks Bhaiya for everything.. Such a gem in coding community

vector<int> rightView(Node *root){
vector<int> res;
if(root==NULL) return res;
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node *temp = q.front();
res.push_back(temp->data);
int size = q.size();
for(int i=0;i<size;i++){
Node *curr = q.front();
q.pop();
if(curr->right!=NULL) q.push(curr->right);
if(curr->left!=NULL) q.push(curr->left);
}
}
return res;
}

krishnasudan
Автор

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!

Dontpushyour_luck
Автор

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 ♥️🔥

VishalGupta-xwrp
Автор

your explanation techniques are phenomenal, clear concepts and concise code. Loving the way you code.

shubh
Автор

Easy solution for left view level order traversal:


#include <bits/stdc++.h>
using namespace std;

vector<int> getLeftView(TreeNode<int> *root)
{
if (root == NULL)
return {};
vector<int> ans;
queue<TreeNode<int> *> 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;
}

divyansh
Автор

In case anyone want iterative method 😅
class Solution
{
public:
//Function to return list containing elements of right view of binary tree.
vector<int> rightView(Node *root)
{
// Your Code here
vector<int>res;
if(!root)
return res;
queue<Node*>q;

q.push(root);
while(!q.empty()){
int n=q.size();
for(int i=0;i<n;i++){


if(i==0){

}


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 😍😍

PalakMittal
Автор

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<int> rightSideView(TreeNode* root) {
vector<int> ans;
if(root==nullptr) return {};
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i=0;i<size;i++){
TreeNode* front = q.front();
if(i==size-1){
ans.push_back(front->val);
}
q.pop();
if(front->left!=nullptr) q.push(front->left);
if(front->right!=nullptr) q.push(front->right);
}
}
return ans;
}
};

mulate
Автор

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 ✌

falgunitagadkar
Автор

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.

samarthsingh
Автор

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);

rohan
Автор

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);

rajnishism
Автор

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

yogeshjoshi
Автор

Here is the level order traversal for right side view:
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*>q;
q.push(root);
vector<int>ans;
if(root==NULL)
return ans;
while(!q.empty()){
int size=q.size();
int a=0;
for(int i=0;i<size;i++){
TreeNode* node=q.front();
q.pop();
a=node->val;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
ans.push_back(a);
}return ans;
}
};

maradanikhil
join shbcf.ru