Binary Tree Right Side View @LeetCode
PROBLEM :
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
if(root==NULL)
return vec ;
int height=heightTree(root) ;
for(int i=0;i<height;i++){
bool status=true ;
rightView(root,vec,i,&status) ;
}
return vec ;
}
int heightTree(TreeNode* root){
if(root==NULL)
return 0 ;
int l=heightTree(root->left) ;
int r=heightTree(root->right) ;
return l>r?l+1:r+1 ;
}
void rightView(TreeNode* root,vector<int> &vec,int level,bool *status){
if(root==NULL||!(*status))
return ;
if(level==0){
vec.push_back(root->val) ;
(*status)=false ;
}
else{
rightView(root->right,vec,level-1,&(*status)) ;
rightView(root->left,vec,level-1,&(*status)) ;
}
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Better Solution - Recursive)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
RightView(root,vec,1) ;
return vec ;
}
void RightView(TreeNode* root,vector<int> &vec,int level){
if(root==NULL)
return ;
if(vec.size()<level)
vec.push_back(root->val) ;
RightView(root->right,vec,level+1) ;
RightView(root->left,vec,level+1) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Better Solution - Iterative)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
if(root==NULL)
return vec ;
queue<TreeNode*> que ;
que.push(root) ;
bool status ;
while(!que.empty()){
int size=que.size() ;
status=true ;
while(size--){
TreeNode* curr=que.front() ;
que.pop() ;
if(status){
vec.push_back(curr->val) ;
status=false ;
}
if(curr->right)
que.push(curr->right) ;
if(curr->left)
que.push(curr->left) ;
}
}
return vec ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
if(root==NULL)
return vec ;
int height=heightTree(root) ;
for(int i=0;i<height;i++){
bool status=true ;
rightView(root,vec,i,&status) ;
}
return vec ;
}
int heightTree(TreeNode* root){
if(root==NULL)
return 0 ;
int l=heightTree(root->left) ;
int r=heightTree(root->right) ;
return l>r?l+1:r+1 ;
}
void rightView(TreeNode* root,vector<int> &vec,int level,bool *status){
if(root==NULL||!(*status))
return ;
if(level==0){
vec.push_back(root->val) ;
(*status)=false ;
}
else{
rightView(root->right,vec,level-1,&(*status)) ;
rightView(root->left,vec,level-1,&(*status)) ;
}
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Better Solution - Recursive)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
RightView(root,vec,1) ;
return vec ;
}
void RightView(TreeNode* root,vector<int> &vec,int level){
if(root==NULL)
return ;
if(vec.size()<level)
vec.push_back(root->val) ;
RightView(root->right,vec,level+1) ;
RightView(root->left,vec,level+1) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Better Solution - Iterative)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> vec ;
if(root==NULL)
return vec ;
queue<TreeNode*> que ;
que.push(root) ;
bool status ;
while(!que.empty()){
int size=que.size() ;
status=true ;
while(size--){
TreeNode* curr=que.front() ;
que.pop() ;
if(status){
vec.push_back(curr->val) ;
status=false ;
}
if(curr->right)
que.push(curr->right) ;
if(curr->left)
que.push(curr->left) ;
}
}
return vec ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment