import java.util.LinkedList;
public class LevelOrder
{
public void levelIterator(BiTree root)
if(root == null)
return ;
}
LinkedList<BiTree> queue = new LinkedList<BiTree>();
BiTree current = null;
queue.offer(root);//將根節點入隊
while(!queue.isEmpty())
current = queue.poll();//出隊隊頭元素並訪問
System.out.print(current.val +"-->");
if(current.left != null)//如果當前節點的左節點不為空入隊
queue.offer(current.left);
if(current.right != null)//如果當前節點的右節點不為空,把右節點入隊
queue.offer(current.right);
import java.util.LinkedList;
public class LevelOrder
{
public void levelIterator(BiTree root)
{
if(root == null)
{
return ;
}
LinkedList<BiTree> queue = new LinkedList<BiTree>();
BiTree current = null;
queue.offer(root);//將根節點入隊
while(!queue.isEmpty())
{
current = queue.poll();//出隊隊頭元素並訪問
System.out.print(current.val +"-->");
if(current.left != null)//如果當前節點的左節點不為空入隊
{
queue.offer(current.left);
}
if(current.right != null)//如果當前節點的右節點不為空,把右節點入隊
{
queue.offer(current.right);
}
}
}
}