bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1161. Maximum Level Sum of a Binary Tree

1161. Maximum Level Sum of a Binary Tree

Leetcode Solutions

Breadth First Search (BFS) Approach

  1. Initialize a queue and add the root node to it.
  2. Initialize variables maxSum to a large negative value and ans to 1.
  3. Initialize level to 0.
  4. While the queue is not empty: a. Increment level. b. Initialize sumAtCurrentLevel to 0. c. For each node at the current level (using the size of the queue): i. Dequeue the node from the queue. ii. Add the node's value to sumAtCurrentLevel. iii. Enqueue the node's children (if any) to the queue. d. If sumAtCurrentLevel is greater than maxSum, update maxSum and ans with the current level.
  5. Return ans.
UML Thumbnail

Depth First Search (DFS) Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...