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 kthlargest 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
Solutions
Solution 1: BFS + Sorting
We can use BFS to traverse the binary tree, while recording the sum of nodes at each level, then sort the array of node sums, and finally return the $k$th largest node sum. Note that if the number of levels in the binary tree is less than $k$, then return $-1$.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 9101112131415161718192021
# 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 = rightclassSolution:defkthLargestLevelSum(self,root:Optional[TreeNode],k:int)->int:arr=[]q=deque([root])whileq:t=0for_inrange(len(q)):root=q.popleft()t+=root.valifroot.left:q.append(root.left)ifroot.right:q.append(root.right)arr.append(t)return-1iflen(arr)<kelsenlargest(k,arr)[-1]
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funckthLargestLevelSum(root*TreeNode,kint)int64{arr:=[]int{}q:=[]*TreeNode{root}forlen(q)>0{t:=0forn:=len(q);n>0;n--{root=q[0]q=q[1:]t+=root.Valifroot.Left!=nil{q=append(q,root.Left)}ifroot.Right!=nil{q=append(q,root.Right)}}arr=append(arr,t)}ifn:=len(arr);n>=k{sort.Ints(arr)returnint64(arr[n-k])}return-1}
We can also use DFS to traverse the binary tree, while recording the sum of nodes at each level, then sort the array of node sums, and finally return the $k$th largest node sum. Note that if the number of levels in the binary tree is less than $k$, then return $-1$.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 91011121314151617181920
# 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 = rightclassSolution:defkthLargestLevelSum(self,root:Optional[TreeNode],k:int)->int:defdfs(root,d):ifrootisNone:returniflen(arr)<=d:arr.append(0)arr[d]+=root.valdfs(root.left,d+1)dfs(root.right,d+1)arr=[]dfs(root,0)return-1iflen(arr)<kelsenlargest(k,arr)[-1]
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funckthLargestLevelSum(root*TreeNode,kint)int64{arr:=[]int{}vardfsfunc(*TreeNode,int)dfs=func(root*TreeNode,dint){ifroot==nil{return}iflen(arr)<=d{arr=append(arr,0)}arr[d]+=root.Valdfs(root.Left,d+1)dfs(root.Right,d+1)}dfs(root,0)ifn:=len(arr);n>=k{sort.Ints(arr)returnint64(arr[n-k])}return-1}