Good morning! Here's our prompt for today.
Given a binary tree, write a method to return an array containing the largest (by value) node values at each level. In other words, we're looking for the max per level.

So for instance, given the following binary tree, we'd get [2, 7, 9] if the method grabbed the correct maxes.
JAVASCRIPT
1/*
2 2
3 / \
4 3 7
5 / \ \
6 5 8 9
7*/
8
9maxValPerLevel(root);
10// [2, 7, 9]Assuming the standard tree node definition of:
JAVASCRIPT
1function Node(val) {
2 this.val = val;
3 this.left = this.right = null;
4}Can you fill it out via the following function signature?
JAVASCRIPT
1function maxValPerLevel(root) {
2 // if (!root) { return []; }
3
4 return;
5};Constraints
- The number of nodes in the given tree <=
100000 - The nodes will always contain integer values between
-1000000000and1000000000 - Expected time complexity :
O(n) - Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx92
​class Node: def __init__(self, val): self.val = val self.left = None self.right = None​​def max_per_level(root): # fill this in return root​​# root = Node(2)# root.left = Node(3)# root.right = Node(7)# root.left.left = Node(5)# root.left.right = Node(8)# root.right.right = Node(9)# print(maxValPerLevel(root))​​# Node definitionclass Node: def __init__(self, val): self.val = val self.left = None self.right = None​​OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's a video of us explaining the solution.
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

We'll now take you through what you need to know.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.


