Anonymous inner classes create generic instances that display the dec… (#627)

* Anonymous inner classes create generic instances that display the declared type

* Update TreeNode.java

* Update binary_tree_bfs.java

* Update graph_bfs.java

---------

Co-authored-by: zongjianwei <zongjianwei@meituan.com>
Co-authored-by: Yudong Jin <krahets@163.com>
pull/601/head
ZongYangL 1 year ago committed by GitHub
parent 7a6fd4b3dd
commit 03cbf5b972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,9 +16,11 @@ public class graph_bfs {
// 顶点遍历序列 // 顶点遍历序列
List<Vertex> res = new ArrayList<>(); List<Vertex> res = new ArrayList<>();
// 哈希表,用于记录已被访问过的顶点 // 哈希表,用于记录已被访问过的顶点
Set<Vertex> visited = new HashSet<>() {{ add(startVet); }}; Set<Vertex> visited = new HashSet<>();
visited.add(startVet);
// 队列用于实现 BFS // 队列用于实现 BFS
Queue<Vertex> que = new LinkedList<>() {{ offer(startVet); }}; Queue<Vertex> que = new LinkedList<>();
que.offer(startVet);
// 以顶点 vet 为起点,循环直至访问完所有顶点 // 以顶点 vet 为起点,循环直至访问完所有顶点
while (!que.isEmpty()) { while (!que.isEmpty()) {
Vertex vet = que.poll(); // 队首顶点出队 Vertex vet = que.poll(); // 队首顶点出队

@ -13,7 +13,8 @@ public class binary_tree_bfs {
/* 层序遍历 */ /* 层序遍历 */
static List<Integer> levelOrder(TreeNode root) { static List<Integer> levelOrder(TreeNode root) {
// 初始化队列,加入根节点 // 初始化队列,加入根节点
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }}; Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
// 初始化一个列表,用于保存遍历序列 // 初始化一个列表,用于保存遍历序列
List<Integer> list = new ArrayList<>(); List<Integer> list = new ArrayList<>();
while (!queue.isEmpty()) { while (!queue.isEmpty()) {

@ -26,7 +26,8 @@ public class TreeNode {
return null; return null;
TreeNode root = new TreeNode(list.get(0)); TreeNode root = new TreeNode(list.get(0));
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }}; Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int i = 0; int i = 0;
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
@ -51,7 +52,7 @@ public class TreeNode {
List<Integer> list = new ArrayList<>(); List<Integer> list = new ArrayList<>();
if (root == null) if (root == null)
return list; return list;
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }}; Queue<TreeNode> queue = new LinkedList<TreeNode>() {{ add(root); }};
while (!queue.isEmpty()) { while (!queue.isEmpty()) {
TreeNode node = queue.poll(); TreeNode node = queue.poll();
if (node != null) { if (node != null) {

Loading…
Cancel
Save