最常见的程序员面试题(2)二叉树逐层打印
来源:互联网 发布:炉石传说盒子数据在哪 编辑:程序博客网 时间:2024/06/06 10:41
二叉树的遍历都不能逐层访问。考虑用一个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();}}}
- 最常见的程序员面试题(2)二叉树逐层打印
- 最常见的程序员面试题(7)二叉树转链表
- 最常见的程序员面试题(10)字符串的处理
- 最常见的程序员面试题(11)排序二叉树的序列化和反序列化
- 最常见的程序员面试题(1)单链表反向
- 最常见的程序员面试题(3)置换算法
- 最常见的程序员面试题(8)常见的几种递归和堆栈问题
- 最常见的程序员面试题(6)计算最常用的URL地址
- 常见的二叉树面试题
- 二叉树的常见面试题总结
- 最常见的php面试题
- 常见程序员面试题
- 最常见的程序员面试题(4)写一个能工作的Singleton
- 最常见的程序员面试题(9)求和/求极值的问题
- 最常见的程序员面试题(5)Java/C++线程同步
- 最常见的程序员面试题(12)观察者模式和weak_ptr
- 二叉树常见面试题
- 二叉树常见面试题
- 最常见的程序员面试题(1)单链表反向
- Spring Extension (2) — Annotation RequestAttribute for Controller method Parameter Injection
- CFString去掉string中的字符串(空格)
- 一个有趣的Javascript波浪特效
- openstack实际应用中问题总结
- 最常见的程序员面试题(2)二叉树逐层打印
- The Quiz(http://dmitrysoshnikov.com/ecmascript/the-quiz/#q9)
- 最常见的程序员面试题(3)置换算法
- 最常见的程序员面试题(4)写一个能工作的Singleton
- 最常见的程序员面试题(5)Java/C++线程同步
- 以前进行的程序安装创建了挂起的文件操作(SqlServer2000或SqlServer 2000 SP4补丁安装) .
- [poj]1002.487-3279
- top命令参数解释
- 最常见的程序员面试题(6)计算最常用的URL地址