按层遍历二叉树 并不分行打印
方法1: 用双向队列辅助
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> result;
if(root == nullptr)
return result;
deque<TreeNode*> sup;
sup.push_back(root);
while(sup.size()){
TreeNode* pNode = sup.front();
result.push_back(pNode->val);
sup.pop_front();
if(pNode->left)
sup.push_back(pNode->left);
if(pNode->right)
sup.push_back(pNode->right);
}
return result;
}
};
方法2:单向队列
入队不判空出队判空的缺点是需要进行多次出队判空操作 优点是代码简洁
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode *rt) {
queue<TreeNode*> q;
q.push(rt);
vector<int> r;
while(!q.empty()){
rt = q.front();
q.pop();
//出队时判空
if(!rt) continue;
//非空则插入动态数组
r.push_back(rt -> val);
//对入队列不进行判空
q.push(rt -> left);
q.push(rt -> right);
}
return r;
}
};
按层遍历并分行打印
第一种 入不看空 出判空
void Printabc(BinaryTreeNode* pRoot){
std::queue<BinaryTreeNode*> q;
q.push(pRoot);
int curline=1;
int nextline=0;
while(q.size()){
pRoot = q.front();
q.pop();
curline--;
if(!pRoot){
if(curline==0)
{
std::cout<<'\n';
curline=nextline;
nextline = 0;
}
continue;
}
std::cout << pRoot->m_nValue;
q.push(pRoot->m_pLeft);
q.push(pRoot->m_pRight);
nextline+=2;
if(curline==0)
{
std::cout<<'\n';
curline=nextline;
nextline = 0;
}
}
return;
}
//入判空
void Printo(BinaryTreeNode* pRoot){
if(pRoot == nullptr)
return;
std::queue<BinaryTreeNode*> q;
q.push(pRoot);
int curline = 1;
int nextline = 0;
while(q.size())
{
pRoot = q.front();
q.pop();
curline--;
std::cout<<pRoot->m_nValue;
if(pRoot->m_pLeft)
{
nextline++;
q.push(pRoot->m_pLeft);
}
if(pRoot->m_pRight)
{
nextline++;
q.push(pRoot->m_pRight);
}
if(curline==0){
std::cout<<'\n';
curline = nextline;
nextline = 0;
}
}
return;
}
按层遍历二叉树 按行打印 Z字打印 从左到右 从右到左交叉
2个栈
void Printabc(BinaryTreeNode* pRoot){
if(pRoot == nullptr)
return;
std::stack<BinaryTreeNode*> sup[2];
int current = 0;
int next = 1;
sup[current].push(pRoot);
while(sup[0].size()>0||sup[1].size()>0){
pRoot=sup[current].top();
sup[current].pop();
std::cout<<pRoot->m_nValue<<' ';
if(current==1) //从右子树到左子树
{
if(pRoot->m_pRight)
sup[next].push(pRoot->m_pRight);
if(pRoot->m_pLeft)
sup[next].push(pRoot->m_pLeft);
}
else//从左子树到右子树
{
if(pRoot->m_pLeft)
sup[next].push(pRoot->m_pLeft);
if(pRoot->m_pRight)
sup[next].push(pRoot->m_pRight);
}
if(sup[current].empty()){
current = 1- current;
next = 1-next;
std::cout<<'\n';
}
}
return;
}
/**********************之字形打印二叉树********************************/
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > res;
if(!pRoot)
return res;
stack<TreeNode* > sup[2];
int current = 1;
int next = 0;
sup[current].push(pRoot);
while( sup[0].size()||sup[1].size())
{
vector<int> tmp;
while(sup[current].size())
{
pRoot = sup[current].top();
sup[current].pop();
tmp.push_back(pRoot->val);
if(current) //right to left
{
if(pRoot->left)
sup[next].push(pRoot->left);
if(pRoot->right)
sup[next].push(pRoot->right);
}
else
{
if(pRoot->right)
sup[next].push(pRoot->right);
if(pRoot->left)
sup[next].push(pRoot->left);
}
}
res.push_back(tmp);
current = 1 - current;
next = 1- next;
}
return res;
}
};