二叉树的遍历都不能逐层访问。考虑用一个FIFO队列: 压入树根,然后:
(1) 每次取出一个节点,打印数据。压入节点的左右子树
(2) 删除队列头节点。
(3) 循环(1)-(2)直到队列为空。
用C++和Java分别实现。
- #include <deque>
- struct node{
- explicit node( int d )
- : data( d ), pLeft( nullptr ), pRight( nullptr )
- {}
- ~node(){
- delete pLeft;
- delete pRight;
- }
- int data;
- node* pLeft;
- node* pRight;
- node* appendLeft( node* pn ){
- if( pLeft )delete pLeft;
- pLeft = pn;
- return pLeft;
- }
- node* appendRight( node* pn ){
- if( pRight )delete pRight;
- pRight = pn;
- return pRight;
- }
- };
- void printLayers( node& n ){
- std::deque<node*> di;
- di.push_back(&n);
- while(di.size()){
- node* pn=di.front();
- printf("%d,",pn->data);
- if(pn->pLeft) di.push_back(pn->pLeft);
- if(pn->pRight)di.push_back(pn->pRight);
- di.pop_front();
- }
- }
- int main(void){
- node root(1);
- node* r_left=root.appendLeft( new node(2) );
- node* r_right=root.appendRight( new node(3) );
- node* lr=r_left->appendRight( new node(5) );
- node* ll=r_left->appendLeft( new node(4) );
- node* rl=r_right->appendLeft( new node(6) );
- node* rr=r_right->appendRight( new node(7) );
- printLayers( root );
- return 0;
- }
#include <deque>
struct node{
explicit node( int d )
: data( d ), pLeft( nullptr ), pRight( nullptr )
{}
~node(){
delete pLeft;
delete pRight;
}
int data;
node* pLeft;
node* pRight;
node* appendLeft( node* pn ){
if( pLeft )delete pLeft;
pLeft = pn;
return pLeft;
}
node* appendRight( node* pn ){
if( pRight )delete pRight;
pRight = pn;
return pRight;
}
};
void printLayers( node& n ){
std::deque<node*> di;
di.push_back(&n);
while(di.size()){
node* pn=di.front();
printf("%d,",pn->data);
if(pn->pLeft) di.push_back(pn->pLeft);
if(pn->pRight)di.push_back(pn->pRight);
di.pop_front();
}
}
int main(void){
node root(1);
node* r_left=root.appendLeft( new node(2) );
node* r_right=root.appendRight( new node(3) );
node* lr=r_left->appendRight( new node(5) );
node* ll=r_left->appendLeft( new node(4) );
node* rl=r_right->appendLeft( new node(6) );
node* rr=r_right->appendRight( new node(7) );
printLayers( root );
return 0;
}
- import java.util.*;
- public class Revert {
- public static void main(String[] args) {
- node root=new node(1);
- node r=root.appendRight(new node(3));
- node l=root.appendLeft(new node(2));
- r.appendLeft(new node(6));
- r.appendRight(new node(7));
- l.appendRight(new node(5));
- l.appendLeft(new node(4));
- Revert.printLayer( root );
- }
- static class node{
- int data;
- node left;
- node right;
- node( int d ){
- data = d;
- left = null;
- right = null;
- }
- node appendLeft( node pn ){
- left = pn;
- return left;
- }
- node appendRight( node pn ){
- right = pn;
- return right;
- }
- }
- static void printLayer( node root ){
- LinkedList<node> li=new LinkedList<node>();
- li.push( root );
- while( li.size() != 0 ){
- node n=li.peekFirst();
- System.out.println(","+n.data);
- if(n.left !=null)li.addLast(n.left);
- if(n.right!=null)li.addLast(n.right);
- li.removeFirst();
- }
- }
- }
import java.util.*;
public class Revert {
public static void main(String[] args) {
node root=new node(1);
node r=root.appendRight(new node(3));
node l=root.appendLeft(new node(2));
r.appendLeft(new node(6));
r.appendRight(new node(7));
l.appendRight(new node(5));
l.appendLeft(new node(4));
Revert.printLayer( root );
}
static class node{
int data;
node left;
node right;
node( int d ){
data = d;
left = null;
right = null;
}
node appendLeft( node pn ){
left = pn;
return left;
}
node appendRight( node pn ){
right = pn;
return right;
}
}
static void printLayer( node root ){
LinkedList<node> li=new LinkedList<node>();
li.push( root );
while( li.size() != 0 ){
node n=li.peekFirst();
System.out.println(","+n.data);
if(n.left !=null)li.addLast(n.left);
if(n.right!=null)li.addLast(n.right);
li.removeFirst();
}
}
}