Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.38 KB

_2583. Kth Largest Sum in a Binary Tree.md

File metadata and controls

51 lines (37 loc) · 1.38 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : October 22, 2024

Last updated : October 22, 2024


Related Topics : Tree, Breadth-First Search, Sorting, Binary Tree

Acceptance Rate : 59.38 %


Solutions

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:
        def dfs(curr: Optional[TreeNode], dep: int = 0, sums: List[int] = []) -> List[int] :
            if not curr :
                return sums
            if dep >= len(sums) :
                sums.append(0)

            sums[dep] += curr.val

            dfs(curr.left, dep + 1, sums)
            dfs(curr.right, dep + 1, sums)

            return sums
        sums = sorted(dfs(root))
        return -1 if k > len(sums) else sums[-k]