Here is the code for BFS for your Binary Tree.
public void funcBFS(BTNode root){
if (root == null)
return;
Queue<BTNode> q = new Queue<BTNode>();
q.enqueue(root);
while(!q.isEmpty()){
BTNode n = q.dequeue();
System.out.print(n.data+" ");
if(n.left != null)
q.enqueue(n.left);
if(n.right != null)
q.enqueue(n.right);
}
}
网友评论