Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Largest Value in Each Tree Row

Medium Acceptance 50.00% Points 30.00

Given a binary tree, find the largest value in each level.

Examples
Example 1

Example 1

Input: root[] = [1, 2, 3]

Output: [1, 3]

Explanation: First traverse the first level then second level.

Example 2

Example 2

Input: root = [3,9,20,N,N,15,7]

Output: [3, 20, 15]

Explanation: Traverse the tree level wise, first traverse the first level, then second and so on.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Auxiliary Space: O(n).
Constraints
  • 1 = number of nodes = 10^5
  • 0 = node->data = 10^9
Companies
Amazon Microsoft Google DE Shaw Flipkart
Topics
Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions