BFS of binary tree *** Processing the whole level at the same time by taking size of the queue
BFS0.java *** processing only single node at a time
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
if (root == null) return result;
queue.add(root);
while (queue.size() != 0) {
TreeNode cur = queue.poll();
System.out.println(cur.val);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
return result;
}
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
if (root == null) return result;
queue.add(root);
while (queue.size() != 0) {
int size = queue.size();
for (int i=0; i<size; i++) {
TreeNode cur = queue.poll();
System.out.println(cur.val);
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
}
}
return result;
}