import javax.swing.tree.TreeNode;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Created by heziyu-dx on 17/10/26.
*/
/**
* 层序遍历需要个队列来辅助,这里只是个大概意思
*/
public class cengxubianli {
Queue<TreeNode1> queue=new LinkedBlockingQueue<TreeNode1>();
public void cengxu(TreeNode1 root){
System.out.println(root);
queue.add(root.left);
if(root.left!=null){
queue.offer(root.left);
}
if(root.right!=null){
queue.offer(root.right);
}
while(queue.size()>0){
TreeNode1 t1=queue.poll();
System.out.println(queue.poll());
if(t1.left!=null)queue.offer(t1.left);
if(t1.right!=null)queue.offer(t1.right);
}
System.out.println("end");
}
class TreeNode1{
private TreeNode1 left;
private TreeNode1 right;
}
}
二叉树的层序遍历
最新推荐文章于 2024-08-28 23:39:16 发布