MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / levelOrder

Method levelOrder

TreeLevelOrder.java:2–20  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

1class Solution {
2 public List<List<Integer>> levelOrder(TreeNode root) {
3 List<List<Integer>> list = new ArrayList<List<Integer>>();
4 if(root==null) return list;
5 Queue<TreeNode> queue = new LinkedList<TreeNode>();
6 queue.offer(root);
7 while(!queue.isEmpty())
8 {
9 int nodesPresent=queue.size();
10 List<Integer> sublist = new ArrayList<Integer>();
11 for(int i=0;i<nodesPresent;i++)
12 {
13 if(queue.peek().left!=null) queue.offer(queue.peek().left);
14 if(queue.peek().right!=null) queue.offer(queue.peek().right);
15 sublist.add(queue.poll().val);
16 }
17 list.add(sublist);
18 }
19 return list;
20 }
21}

Callers

nothing calls this directly

Calls 2

isEmptyMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected