Binary Tree Level Order Traversal @LeetCode
PROBLEM :
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive 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>> levelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
LevelTraversal(root,vec,0) ;
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 :( Iterative Solution - QUEUE )
--------------------------------------------------------------------------------
/**
* 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>> levelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
if(height==0)
return vec ;
int curr=0 ;
queue<TreeNode*> que ;
que.push(root) ;
while(!que.empty()){
int size=que.size() ;
while(size--){
TreeNode* temp ;
temp=que.front() ;
que.pop() ;
vec[curr].push_back(temp->val);
if(temp->left)
que.push(temp->left) ;
if(temp->right)
que.push(temp->right) ;
}
curr++ ;
}
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 ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :( Recursive 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>> levelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
LevelTraversal(root,vec,0) ;
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 :( Iterative Solution - QUEUE )
--------------------------------------------------------------------------------
/**
* 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>> levelOrder(TreeNode* root) {
int height=FindHeight(root) ;
vector<vector<int>> vec(height,vector<int> {});
if(height==0)
return vec ;
int curr=0 ;
queue<TreeNode*> que ;
que.push(root) ;
while(!que.empty()){
int size=que.size() ;
while(size--){
TreeNode* temp ;
temp=que.front() ;
que.pop() ;
vec[curr].push_back(temp->val);
if(temp->left)
que.push(temp->left) ;
if(temp->right)
que.push(temp->right) ;
}
curr++ ;
}
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 ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment