编程之美-----分层遍历二叉树
1. 简述
问题一:给定一棵二叉树,要求按分层遍历该二叉树,即从上到下的层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。
问题二:写一个函数,打印二叉树中某层次的节点(从左到右),其中根节点为第0层,函数原型为int PrintNodeAtLevel(Node* root, int level),成功返回1,失败返回0。
方法一:利用递归方式,搜寻并打印某一层的节点,再打印下一层的节点。这方法简单但时间效率不高(但不需要额外空间).
代码为:
#include<iostream>
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
int PrintTreeByLevel(Node* root,int level)
{
if(!root||level<0)
return 0;
if(level==0)
{
cout<<root->value<<" ";
return 1;
}
return PrintTreeByLevel(root->pLeft,level-1)+PrintTreeByLevel(root->pRight,level-1);
}
void PrintNodeByLevel(Node *root)
{
for(int level=0; ; level++)
{
if(!PrintTreeByLevel(root,level))
break;
cout<<endl;
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
PrintNodeByLevel(array[0]);
system("pause");
return 0;
}
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
int PrintTreeByLevel(Node* root,int level)
{
if(!root||level<0)
return 0;
if(level==0)
{
cout<<root->value<<" ";
return 1;
}
return PrintTreeByLevel(root->pLeft,level-1)+PrintTreeByLevel(root->pRight,level-1);
}
void PrintNodeByLevel(Node *root)
{
for(int level=0; ; level++)
{
if(!PrintTreeByLevel(root,level))
break;
cout<<endl;
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
PrintNodeByLevel(array[0]);
system("pause");
return 0;
}
方法二:从根节点出发,依次将每层的节点从左到右压入一个数组,并用一个游标Cur记录当前访问的节点,另一个游标Last指示当前层次的最后一个节点的下一个位置,以Cur==Last作为当前层次访问结束的条件,在访问某一层的同时将该层的所有节点的子节点压入数组,在访问完某一层之后,检查是否还有新的层次可以访问,直到访问完所有的层次.
代码为:
#include<iostream>
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
void PrintTreeByLevel(Node* root)
{
if(root==NULL)
return;
vector<Node*> vec;
vec.push_back(root);
int cur=0;
int last=1;
while(cur<vec.size())
{
last=vec.size();
while(cur<last)
{
cout<<vec[cur]->value<<" ";
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
cur++;
}
cout<<endl;
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
PrintTreeByLevel(array[0]);
system("pause");
return 0;
}
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
void PrintTreeByLevel(Node* root)
{
if(root==NULL)
return;
vector<Node*> vec;
vec.push_back(root);
int cur=0;
int last=1;
while(cur<vec.size())
{
last=vec.size();
while(cur<last)
{
cout<<vec[cur]->value<<" ";
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
cur++;
}
cout<<endl;
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
PrintTreeByLevel(array[0]);
system("pause");
return 0;
}
3.扩展问题
如果要求按深度从下到上访问二叉树,每层的访问顺序仍然是从左向右,如果从右向左呢?
#include<iostream>
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
vector<Node *> vec;
void step(Node* root,bool fromLeftToRight) //true:从左往右遍历 //false:从右往左遍历
{
int cur=0;
int last=1;
vec.clear();
vec.push_back(root);
while(cur<vec.size())
{
last=vec.size();
vec.push_back(NULL);
while(cur<last)
{
if(fromLeftToRight)
{
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
}
else
{
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
}
cur++;
}
cur++;
}
vector<Node*>::reverse_iterator reverse;
for(reverse=vec.rbegin()+1;reverse!=vec.rend();reverse++)
{
if(!*reverse)
{
cout<<endl;
}
else
{
cout<<(*reverse)->value<<" ";
}
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
step(array[0],1);
step(array[0],0);
system("pause");
return 0;
}
#include<vector>
using namespace std;
struct Node{
Node* pLeft;
Node* pRight;
int value;
};
vector<Node *> vec;
void step(Node* root,bool fromLeftToRight) //true:从左往右遍历 //false:从右往左遍历
{
int cur=0;
int last=1;
vec.clear();
vec.push_back(root);
while(cur<vec.size())
{
last=vec.size();
vec.push_back(NULL);
while(cur<last)
{
if(fromLeftToRight)
{
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
}
else
{
if(vec[cur]->pLeft)
vec.push_back(vec[cur]->pLeft);
if(vec[cur]->pRight)
vec.push_back(vec[cur]->pRight);
}
cur++;
}
cur++;
}
vector<Node*>::reverse_iterator reverse;
for(reverse=vec.rbegin()+1;reverse!=vec.rend();reverse++)
{
if(!*reverse)
{
cout<<endl;
}
else
{
cout<<(*reverse)->value<<" ";
}
}
}
int main()
{
Node* array[8];
for(int i=0;i<8;i++)
{
array[i]=new Node;
array[i]->value=1+i;
array[i]->pLeft=array[i]->pRight=NULL;
}
array[0]->pLeft = array[1]; array[0]->pRight = array[2];
array[1]->pLeft = array[3]; array[1]->pRight = NULL;
array[2]->pLeft = array[4]; array[2]->pRight = array[5];
array[4]->pLeft = array[6]; array[4]->pRight = array[7];
step(array[0],1);
step(array[0],0);
system("pause");
return 0;
}
4.不需要额外空间的简明算法
第一个尝试,利用了两个队列,一个储存本层的节点,另一个储存下层的节点。遍历本层的节点,把其子代节点排入下层队列。本层遍历完毕后,就可换行,并交换两个队列。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void
PrintNodeByLevel(Node* root) {
deque<Node*> Q1, Q2;
Q1.push_back(root);
do
{
do
{
Node* node = Q1.front();
Q1.pop_front();
cout << node->data <<
" "
;
if
(node->pLeft)
Q2.push_back(node->pLeft);
if
(node->pRight)
Q2.push_back(node->pRight);
}
while
(!Q1.empty());
cout << endl;
Q1.swap(Q2);
}
while
(!Q1.empty());
}
|
本实现使用deque而不是queue,因为deque才支持swap()操作。注意,swap()是O(1)的操作,实际上只是交换指针。
这实现要用两个循环(书上的实现也是),并且用了两个队列。能够只用一个循环、一个队列么?
换行问题其实在于如何表达一层的结束。书上采用了游标,而第一个尝试则用了两个队列。本人想到第三个可行方案,是把一个结束信号放进队列里。由于使用queue<Node*>,可以插入一个空指针去表示一层的遍历结束。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void
PrintNodeByLevel(Node* root) {
queue<Node*> Q;
Q.push(root);
Q.push(0);
do
{
Node* node = Q.front();
Q.pop();
if
(node) {
cout << node->data <<
" "
;
if
(node->pLeft)
Q.push(node->pLeft);
if
(node->pRight)
Q.push(node->pRight);
}
else
if
(!Q.empty()) {
Q.push(0);
cout << endl;
}
}
while
(!Q.empty());
}
|
这个实现的代码很贴近之前的PrintBFS(),也只有一个循环。注意一点,当发现空指针(结束信号)时,要检查队列内是否还有节点,如果没有的话还插入新的结束信号,则会做成死循环。
参考: <<编程之美>>
http://www.cnblogs.com/miloyip/archive/2010/05/12/binary_tree_traversal.html