Binary Tree Level Order Traversal题目描述:
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).
解释:
针对二叉树的层次遍历,一般采用队列数据结构,首先根节点被压入队列,当根节点从队首弹出时,该根节点的左右节点分别从队尾压入,利用这一原理实现层次遍历,代码如下:
C++代码:
/**
* 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)
{
vector<vector<int>>result;
vector<int>temp;
queue<TreeNode*>que;
if(root==NULL)
{
return result;
}
que.push(root);
while(!que.empty())
{
int size= que.size();
for(int i = 0;i<size;i++)
{
TreeNode*node = que.front();
que.pop();
temp.push_back(node->val);
if(node->left!=NULL)
{
que.push(node->left);
}
if(node->right!=NULL)
{
que.push(node->right);
}
}
result.push_back(temp);
temp.clear();
}
return result;
}
};
Binary Tree Level Order Traversal ||题目描述:
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
解释:
从例子中可以看出,这道题目是上面题目层次遍历的升级版,可以先按照上面题目的思路求出结果,再将结果翻转下就是该题的结果。
C++代码:
/**
* 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>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>>result;
vector<int>temp;
queue<TreeNode*>que;
if(root==NULL)
{
return result;
}
que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0 ;i<size;i++)
{
TreeNode *node = que.front();
que.pop();
temp.push_back(node->val);
if(node->left)
{
que.push(node->left);
}
if(node->right)
{
que.push(node->right);
}
}
result.push_back(temp);
temp.clear();
}
reverse(result.begin(),result.end());
return result;
}
};
运行结果:
Binary Tree Zigzag Level Order Traversal题目描述:
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],
C++代码:
/**
* 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>>result;
vector<int>temp;
queue<TreeNode*>que;
if(root==NULL)
{
return result;
}
que.push(root);
while(!que.empty())
{
int size = que.size();
for(int i = 0;i<size;i++)
{
TreeNode *node = que.front();
que.pop();
temp.push_back(node->val);
if(node->left)
{
que.push(node->left);
}
if(node->right)
{
que.push(node->right);
}
}
result.push_back(temp);
temp.clear();
}
for(int i = 0;i<result.size();i++)
{
if(i%2==1)
{
reverse(result[i].begin(),result[i].end());
}
}
return result;
}
};
运行结果: