# Verbose Java Solution, Binary tree level order traversal, again.

• Alright, two binary tree level order traversal problems in one contest. This time, mission is to find the `max` of each level...

``````public class Solution {
public int[] findValueMostElement(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return new int[0];

while (!queue.isEmpty()) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
max = Math.max(max, node.val);
}
}

int[] result = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
result[i] = res.get(i);
}

return result;
}
}
``````

• Hi @shawngao , Thanks for sharing the solution. Just wonder what's the space complexity of this solution. Is it O(n) since the worst case we have only 1 node we will need to add it to the queue? Thanks.

• @ning2016 Yes. Both runtime and space complexity are O(n).

• Similar solution, but separated level traversal from max logic:

``````public class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> results = new ArrayList<>();
return results;
}
private void levelTraversal(TreeNode root, Consumer<List<Integer>> consumer) {
for (Queue<TreeNode> queue = new LinkedList<>(Arrays.asList(root, null)); root != null && !queue.isEmpty(); ) {
TreeNode next = queue.remove();
if (next == null) {
consumer.accept(level);
level.clear();
if (!queue.isEmpty())
} else {
}
}
}
}
``````

• very good, I also want to use sequence traversal the tree, and find the very line max number.

but I use two queues to do this. So result is out of memory.

• dude,we almost have the exactly same code in these most recent problems.....

• Same to me ^ ^

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.