面试题三十二:从上到下打印二叉树

 
 

要求输出 8,10,6,9,11,5,7
方法一:定义一个队列,把出队的节点的左右子树节点近队,如果左右节点为null则不用进队

 1 void PrintTree(  BinaryTreeNode phead ){  
 2             if(phead==nuull) return ;
 3             Queue<BinaryTreeNode> queue=new Queue();
 4             queue.add(phead);
 5             while( queue.peek()!=null ){
 6                     BinaryTreeNode pNode=queue.poll();  //出队
 7                     System.out.print( pNode.value);
 8 
 9                     //子节点进队
10                     if(pNode.Left!=null)    queue.add(pNode.Left);
11                     if(pNode.Right!=null)   queue.add(pNode.Right);
12             }
13         
14     }

分层打印二叉树 可增加两个变量,一个用来表示当前层中还没有打印的数量,一个表示下一层的数量

 void PrintTree(  BinaryTreeNode phead ){
            if(phead==nuull) return ;
            Queue<BinaryTreeNode> queue=new Queue();
            queue.add(phead);

            int nextPrint =0;  //下一层未打印节点数量
            int PrintNum=1;  //当前层未打印节点数量
            while( queue.peek()!=null ){
                    BinaryTreeNode pNode=queue.poll();  //出队
                        PrintNum--;
                    System.out.print( pNode.value);

                    //子节点进队
                    if(pNode.Left!=null) {
                           queue.add(pNode.Left);
                            ++nextPrint;
                     }
                    if(pNode.Right!=null)   {
                            queue.add(pNode.Right);
                            ++nextPrint;
                    }
                    if(PrintNum==0){
                            System.out.println( );
                             PrintNum=nextPrint;;
                             nextPrint;=0;
                    }              
            }
        }

之字形打印二叉树 l例如: 8,6,10,9,11,5,7
分析总结:按之字形顺序打印二叉树需要两个栈
如果打印奇数层,则先保存左节点再保存右节点到另一个栈;
如果打印偶数层,则先保存右节点再保存左节点到另一个栈;

 1 void PrintTree(  BinaryTreeNode phead ){
 2         if(phead==nuull) return ;
 3         
 4         Stack<Integer> s0=new Stack(); //奇数
 5        Stack<Integer> s1=new Stack(); //偶数
 6         
 7         s0.push(phead);
 8         
 9         while( !s1.empty() || !s0.empty() ){
10                 
11                     while(!s0.empty() ){
12                            BinaryTreeNode pNode=s0.pop();
13                             System.out.print( pNode.value );
14                         //先左后右 
15                          if(pNode.Letf)!=null)       s1.push(pNode.Letf) ;
16                          iff(pNode.Right)!=null)         s1.push(pNode.Right );  
17                     }
18                    System.out.println();
19                     while(!s1.empty() ){
20                           BinaryTreeNode pNode=s1.pop();
21                            System.out.print( pNode.value );
22                         //先右后左
23                          if(pNode.Right!=null)        s0.push(pNode.Right );      
24                          if(pNode.Letf)!=null)     s0.push(pNode.Letf) ;                      
25                     }               
26                  System.out.println();
27                 
28         }
29     }

猜你喜欢

转载自www.cnblogs.com/niliuxiaocheng/p/12592339.html
今日推荐