Skip to content

Practice Kth Largest Sum in a Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2583
LeetCode Problem

Kth Largest Sum in a Binary Tree

Problem Statement

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.

Source: Kth Largest Sum in a Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [5,8,9,2,1,3,7,4,6], k = 2
Output
13

# Example 2

Input
root = [1,2,null,3], k = 1
Output
3

Constraints

The number of nodes in the tree is n.
2 <= n <= 10^5
1 <= Node.val <= 10^6
1 <= k <= n

Related LeetCode Problems

Practice more problems similar to Kth Largest Sum in a Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.