Binary Tree Zigzag Level Order Traversal @LeetCode
PROBLEM :
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
--------------------------------------------------------------------------------
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<vector<int>> zigzagLevelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
LevelTraversal(root,vec,0) ;
for(int i=1;i<height;i+=2){
vector<int> *v=&vec[i] ;
reverse(v->begin(),v->end()) ;
}
return vec ;
}
int FindHeight(TreeNode* root){
if(root==NULL)
return 0 ;
int l=FindHeight(root->left) ;
int r=FindHeight(root->right) ;
return l>r?l+1:r+1 ;
}
void LevelTraversal(TreeNode* root,vector<vector<int>> &vec,int curr){
if(root==NULL)
return ;
vec[curr].push_back(root->val);
LevelTraversal(root->left,vec,curr+1) ;
LevelTraversal(root->right,vec,curr+1) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : ( Better Solution )
--------------------------------------------------------------------------------
/**
* 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<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> vec ;
if(root==NULL)
return vec ;
queue<TreeNode*> que ;
que.push(root) ;
bool status=true ;
while(!que.empty()){
int no=que.size() ;
vector<int> row(no) ;
for(int i=0;i<no;i++){
TreeNode* curr=que.front() ;
que.pop() ;
int index=status?i:(no-i-1) ;
row[index]=curr->val ;
if(curr->left)
que.push(curr->left) ;
if(curr->right)
que.push(curr->right) ;
}
status=!status ;
vec.push_back(row) ;
}
return vec ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
--------------------------------------------------------------------------------
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<vector<int>> zigzagLevelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
LevelTraversal(root,vec,0) ;
for(int i=1;i<height;i+=2){
vector<int> *v=&vec[i] ;
reverse(v->begin(),v->end()) ;
}
return vec ;
}
int FindHeight(TreeNode* root){
if(root==NULL)
return 0 ;
int l=FindHeight(root->left) ;
int r=FindHeight(root->right) ;
return l>r?l+1:r+1 ;
}
void LevelTraversal(TreeNode* root,vector<vector<int>> &vec,int curr){
if(root==NULL)
return ;
vec[curr].push_back(root->val);
LevelTraversal(root->left,vec,curr+1) ;
LevelTraversal(root->right,vec,curr+1) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : ( Better Solution )
--------------------------------------------------------------------------------
/**
* 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<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> vec ;
if(root==NULL)
return vec ;
queue<TreeNode*> que ;
que.push(root) ;
bool status=true ;
while(!que.empty()){
int no=que.size() ;
vector<int> row(no) ;
for(int i=0;i<no;i++){
TreeNode* curr=que.front() ;
que.pop() ;
int index=status?i:(no-i-1) ;
row[index]=curr->val ;
if(curr->left)
que.push(curr->left) ;
if(curr->right)
que.push(curr->right) ;
}
status=!status ;
vec.push_back(row) ;
}
return vec ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment