2583. Kth Largest Sum in a Binary Tree LeetCode Solution

In this guide, you will get 2583. Kth Largest Sum in a Binary Tree LeetCode Solution with the best time and space complexity. The solution to Kth Largest Sum in a Binary Tree 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. Kth Largest Sum in a Binary Tree solution in C++
  4. Kth Largest Sum in a Binary Tree solution in Java
  5. Kth Largest Sum in a Binary Tree solution in Python
  6. Additional Resources
2583. Kth Largest Sum in a Binary Tree LeetCode Solution image

Problem Statement of Kth Largest Sum in a Binary Tree

You are given the root of a binary tree and a positive integer k.
The level sum in the tree is the sum of the values of the nodes that are on the same level.
Return the kth largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.
Note that two nodes are on the same level if they have the same distance from the root.

Example 1:

Input: root = [5,8,9,2,1,3,7,4,6], k = 2
Output: 13
Explanation: The level sums are the following:
– Level 1: 5.
– Level 2: 8 + 9 = 17.
– Level 3: 2 + 1 + 3 + 7 = 13.
– Level 4: 4 + 6 = 10.
The 2nd largest level sum is 13.

Example 2:

Input: root = [1,2,null,3], k = 1
Output: 3
Explanation: The largest level sum is 3.

Constraints:

The number of nodes in the tree is n.
2 <= n <= 105
1 <= Node.val <= 106
1 <= k <= n

See also  1816. Truncate Sentence LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n) \to O(n\log n)
  • Space Complexity: O(n)

2583. Kth Largest Sum in a Binary Tree LeetCode Solution in C++

class Solution {
 public:
  long long kthLargestLevelSum(TreeNode* root, int k) {
    vector<long> levelSums;
    dfs(root, 0, levelSums);
    if (levelSums.size() < k)
      return -1;

    nth_element(levelSums.begin(), levelSums.begin() + k - 1, levelSums.end(),
                greater<>());
    return levelSums[k - 1];
  }

 private:
  void dfs(TreeNode* root, int level, vector<long>& levelSums) {
    if (root == nullptr)
      return;
    if (levelSums.size() == level)
      levelSums.push_back(0);
    levelSums[level] += root->val;
    dfs(root->left, level + 1, levelSums);
    dfs(root->right, level + 1, levelSums);
  }
};
/* code provided by PROGIEZ */

2583. Kth Largest Sum in a Binary Tree LeetCode Solution in Java

class Solution {
  public long kthLargestLevelSum(TreeNode root, int k) {
    List<Long> levelSums = new ArrayList<>();
    dfs(root, 0, levelSums);
    if (levelSums.size() < k)
      return -1;

    Collections.sort(levelSums, Collections.reverseOrder());
    return levelSums.get(k - 1);
  }

  private void dfs(TreeNode root, int level, List<Long> levelSums) {
    if (root == null)
      return;
    if (levelSums.size() == level)
      levelSums.add(0L);
    levelSums.set(level, levelSums.get(level) + root.val);
    dfs(root.left, level + 1, levelSums);
    dfs(root.right, level + 1, levelSums);
  }
}
// code provided by PROGIEZ

2583. Kth Largest Sum in a Binary Tree LeetCode Solution in Python

class Solution:
  def kthLargestLevelSum(self, root: TreeNode | None, k: int) -> int:
    levelSums = []

    def dfs(root: TreeNode | None, level: int) -> None:
      if not root:
        return
      if len(levelSums) == level:
        levelSums.append(0)
      levelSums[level] += root.val
      dfs(root.left, level + 1)
      dfs(root.right, level + 1)

    dfs(root, 0)
    if len(levelSums) < k:
      return -1

    return sorted(levelSums, reverse=True)[k - 1]
# code by PROGIEZ

Additional Resources

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