最常见的程序员面试题(2)二叉树逐层打印

来源:互联网 发布:炉石传说盒子数据在哪 编辑:程序博客网 时间:2024/06/06 10:41
二叉树的遍历都不能逐层访问。考虑用一个FIFO队列: 压入树根,然后:

(1) 每次取出一个节点,打印数据。压入节点的左右子树

(2) 删除队列头节点。

(3) 循环(1)-(2)直到队列为空。

用C++和Java分别实现。

[cpp] view plaincopyprint?
  1. #include <deque>   
  2. struct node{  
  3.     explicit node( int d )  
  4.         : data( d ), pLeft( nullptr ), pRight( nullptr )  
  5.     {}  
  6.     ~node(){  
  7.         delete pLeft;  
  8.         delete pRight;  
  9.     }  
  10.     int data;  
  11.     node* pLeft;  
  12.     node* pRight;  
  13.     node* appendLeft( node* pn ){  
  14.         if( pLeft )delete pLeft;  
  15.         pLeft = pn;  
  16.         return pLeft;  
  17.     }  
  18.     node* appendRight( node* pn ){  
  19.         if( pRight )delete pRight;  
  20.         pRight = pn;  
  21.         return pRight;  
  22.     }  
  23. };  
  24. void printLayers( node& n ){  
  25.     std::deque<node*> di;  
  26.     di.push_back(&n);  
  27.     while(di.size()){  
  28.         node* pn=di.front();  
  29.         printf("%d,",pn->data);  
  30.         if(pn->pLeft) di.push_back(pn->pLeft);  
  31.         if(pn->pRight)di.push_back(pn->pRight);  
  32.         di.pop_front();  
  33.     }  
  34. }  
  35. int main(void){  
  36.     node root(1);  
  37.     node* r_left=root.appendLeft( new node(2) );  
  38.     node* r_right=root.appendRight( new node(3) );  
  39.     node* lr=r_left->appendRight( new node(5) );  
  40.     node* ll=r_left->appendLeft( new node(4) );  
  41.     node* rl=r_right->appendLeft( new node(6) );  
  42.     node* rr=r_right->appendRight( new node(7) );  
  43.     printLayers( root );  
  44.     return 0;  
  45. }  

[java] view plaincopyprint?
  1. import java.util.*;  
  2. public class Revert {  
  3.     public static void main(String[] args) {  
  4.         node root=new node(1);  
  5.         node r=root.appendRight(new node(3));  
  6.         node l=root.appendLeft(new node(2));  
  7.         r.appendLeft(new node(6));  
  8.         r.appendRight(new node(7));  
  9.         l.appendRight(new node(5));  
  10.         l.appendLeft(new node(4));  
  11.         Revert.printLayer( root );  
  12.     }  
  13.     static class node{  
  14.         int data;  
  15.         node left;  
  16.         node right;  
  17.         node( int d ){  
  18.             data = d;  
  19.             left = null;  
  20.             right = null;  
  21.         }  
  22.         node appendLeft( node pn ){  
  23.             left = pn;  
  24.             return left;  
  25.         }  
  26.         node appendRight( node pn ){  
  27.             right = pn;  
  28.             return right;  
  29.         }  
  30.     }  
  31.     static void printLayer( node root ){  
  32.         LinkedList<node> li=new LinkedList<node>();  
  33.         li.push( root );  
  34.         while( li.size() != 0 ){  
  35.             node n=li.peekFirst();  
  36.             System.out.println(","+n.data);  
  37.             if(n.left !=null)li.addLast(n.left);  
  38.             if(n.right!=null)li.addLast(n.right);  
  39.             li.removeFirst();  
  40.         }  
  41.     }  
  42. }