Talentd
FleetCode

3831. Median of a Binary Search Tree Level

Medium86.4% acceptancePremium elsewhere, free here

You are given the root of a Binary Search Tree (BST) and an integer level.

The root node is at level 0. Each level represents the distance from the root.

Return the median value of all node values present at the given level. If the level does not exist or contains no nodes, return -1.

The median is defined as the middle element after sorting the values at that level in non-decreasing order. If the number of values at that level is even, return the upper median (the larger of the two middle elements after sorting).

Example 1:

Input: root = [4,null,5,null,7], level = 2

Output: 7

Explanation:

The nodes at level = 2 are [7]. The median value is 7.

Example 2:

Input: root = [6,3,8], level = 1

Output: 8

Explanation:

The nodes at level = 1 are [3, 8]. There are two possible median values, so the larger one 8 is the answer.

Example 3:

​​​​​​​​​​​​​​

Input: root = [2,1], level = 2

Output: -1

Explanation:

There is no node present at level = 2​​​​​​​, so the answer is -1.

Constraints:

  • The number of nodes in the tree is in the range [1, 2 * 105].
  • 1 <= Node.val <= 106
  • 0 <= level <= 2 * 10​​​​​​​5

Hints open one at a time

Hint 1
Use DFS or BFS to gather the node values at level.
Hint 2
Sort the values at that level; if the list is empty, return -1, otherwise return the upper median.
11:27
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[4,null,5,null,7]
2
Input
[6,3,8]
1
Input
[2,1]
2
Your code is saved on this device.Solution