[数据结构][Tree]Search Binary Tree For Level Order

 1 public class BinaryTreeNode
 2 {
 3     public Object data;
 4     
 5     public BinaryTreeNode leftPointer;
 6     
 7     public BinaryTreeNode rightPointer;
 8     
 9     public BinaryTreeNode(Object data)
10     {
11         this.data = data;
12     }
13     
14 }
15 
16 public class Solution
17 {
18     public void levelOrder(BinaryTreeNode node)
19     {
20          Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
21          
22          queue.add(node);
23          
24          while(queue.isEmpty() == false)
25          {
26              BinaryTreeNode tempNode = queue.remove();
27              
28              visit(tempNode);
29              
30              if(tempNode.leftPointer != null)
31              {
32                  queue.add(tempNode.leftPointer);
33              }
34              if(tempNode.rightPointer != null)
35              {
36                  queue.add(tempNode.rightPointer);
37              }
38          }
39     }
40     
41     public void visit(BinaryTreeNode node)
42     {
43         System.out.println(node.data);
44     }
45     
46 }

 

posted @ 2017-10-15 21:44  StringBuilder  阅读(99)  评论(0)    收藏  举报