MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / bfs

Method bfs

java/0199-binary-tree-right-side-view.java:10–22  ·  view source on GitHub ↗
(List<Integer> list, TreeNode root)

Source from the content-addressed store, hash-verified

8 }
9
10 public void bfs(List<Integer> list, TreeNode root) {
11 Queue<TreeNode> q = new LinkedList<>();
12 q.offer(root);
13 while (!q.isEmpty()) {
14 int levelSize = q.size();
15 for (int i = 0; i < levelSize; i++) {
16 TreeNode cur = q.poll();
17 if (i == 0) list.add(cur.val);
18 if (cur.right != null) q.offer(cur.right);
19 if (cur.left != null) q.offer(cur.left);
20 }
21 }
22 }
23}

Callers 1

rightSideViewMethod · 0.95

Calls 3

isEmptyMethod · 0.45
sizeMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected