515. Find Largest Value in Each Tree Row LeetCode Solution

In this guide, you will get 515. Find Largest Value in Each Tree Row LeetCode Solution with the best time and space complexity. The solution to Find Largest Value in Each Tree Row problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Largest Value in Each Tree Row solution in C++
  4. Find Largest Value in Each Tree Row solution in Java
  5. Find Largest Value in Each Tree Row solution in Python
  6. Additional Resources
515. Find Largest Value in Each Tree Row LeetCode Solution image

Problem Statement of Find Largest Value in Each Tree Row

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).

Example 1:

Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]

Example 2:

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

Constraints:

The number of nodes in the tree will be in the range [0, 104].
-231 <= Node.val <= 231 – 1

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

515. Find Largest Value in Each Tree Row LeetCode Solution in C++

class Solution {
 public:
  vector<int> largestValues(TreeNode* root) {
    if (root == nullptr)
      return {};

    vector<int> ans;
    queue<TreeNode*> q{{root}};

    while (!q.empty()) {
      int mx = INT_MIN;
      for (int sz = q.size(); sz > 0; --sz) {
        TreeNode* node = q.front();
        q.pop();
        mx = max(mx, node->val);
        if (node->left)
          q.push(node->left);
        if (node->right)
          q.push(node->right);
      }
      ans.push_back(mx);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

515. Find Largest Value in Each Tree Row LeetCode Solution in Java

class Solution {
  public List<Integer> largestValues(TreeNode root) {
    if (root == null)
      return new ArrayList<>();

    List<Integer> ans = new ArrayList<>();
    Queue<TreeNode> q = new ArrayDeque<>(List.of(root));

    while (!q.isEmpty()) {
      int mx = Integer.MIN_VALUE;
      for (int sz = q.size(); sz > 0; --sz) {
        TreeNode node = q.poll();
        mx = Math.max(mx, node.val);
        if (node.left != null)
          q.offer(node.left);
        if (node.right != null)
          q.offer(node.right);
      }
      ans.add(mx);
    }

    return ans;
  }
}
// code provided by PROGIEZ

515. Find Largest Value in Each Tree Row LeetCode Solution in Python

class Solution:
  def largestValues(self, root: TreeNode | None) -> list[int]:
    if not root:
      return []

    ans = []
    q = collections.deque([root])

    while q:
      mx = -math.inf
      for _ in range(len(q)):
        root = q.popleft()
        mx = max(mx, root.val)
        if root.left:
          q.append(root.left)
        if root.right:
          q.append(root.right)
      ans.append(mx)

    return ans
# code by PROGIEZ

Additional Resources

See also  1016. Binary String With Substrings Representing 1 To N LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.