30.Binary Tree Level Order Traversal(Difficult)
02 Jul 2020 | Daily Algorithms
Pre-inorder Traversal method
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
vector<vector<int>> level;
class Solution {
public:
void building_level(TreeNode* root, int n)
{
if(root==NULL) return;
if(level.size() == n)
{
level.push_back(vector<int>()); // create array in vector
}
level[n].push_back(root->val);
building_level(root->left, n+1);
building_level(root->right, n+1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
building_level(root, 0);
return level;
}
};
Iteration Method
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root)
return {};
vector<int> row;
vector<vector<int>> result;
queue<TreeNode*> q;
q.push(root);
int count = 1;
while(!q.empty())
{
if(q.front()->left)
q.push(q.front()->left);
if(q.front()->right)
q.push(q.front()->right);
row.push_back(q.front()->val);
q.pop();
if(--count==0)
{
result.push_back(row);
row.clear();
count = q.size();
}
}
return result;
}
};
- Tree문제를 풀떄는 가시적으로 보자. [숲을 보지말고 나무를 보자]
Pre-inorder Traversal method
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
vector<vector<int>> level;
class Solution {
public:
void building_level(TreeNode* root, int n)
{
if(root==NULL) return;
if(level.size() == n)
{
level.push_back(vector<int>()); // create array in vector
}
level[n].push_back(root->val);
building_level(root->left, n+1);
building_level(root->right, n+1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
building_level(root, 0);
return level;
}
};
Iteration Method
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root)
return {};
vector<int> row;
vector<vector<int>> result;
queue<TreeNode*> q;
q.push(root);
int count = 1;
while(!q.empty())
{
if(q.front()->left)
q.push(q.front()->left);
if(q.front()->right)
q.push(q.front()->right);
row.push_back(q.front()->val);
q.pop();
if(--count==0)
{
result.push_back(row);
row.clear();
count = q.size();
}
}
return result;
}
};
- Tree문제를 풀떄는 가시적으로 보자. [숲을 보지말고 나무를 보자]
Comments