ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 广度优先遍历(bfs) ![](https://img.kancloud.cn/eb/d6/ebd6a30c9502f2c242488db7c208e7b0_896x722.png) ~~~ // 广度度优先遍历 public static void bfs(TreeNode node){ // 基于队列实现 Queue<TreeNode> queue = new LinkedList<>(); queue.offer(node); while(!queue.isEmpty()){ TreeNode cur = queue.poll(); System.out.print(cur.val+"->"); if(cur.left != null){ queue.offer(cur.left); } if(cur.right != null){ queue.offer(cur.right); } } } ~~~ ``` 宽度优先遍历:(wfs) 1->2->3->4->5->6->7-> ```