二分搜索树广度优先遍历的实现:
/*
* 二分搜索树的层序遍历(广度优先遍历),队列实现
* 广度优先遍历优势在于更快找到想要查询的元素,主要用于搜索策略,算法设计--最短路径(无权图)
*/
public void levelOrder(){
Queue<Node> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
Node cur = q.remove();
System.out.println(cur.e);
if(cur.left!=null){
q.add(cur.left);
}
if(cur.right!=null){
q.add(cur.right);
}
}
}
测试:

转载于:https://www.cnblogs.com/ltfxy/p/10004544.html