root of a binary tree and a positive integer rank.
The *weight* of a level is the sum of the values of all nodes at the same distance from the root. Sort the weights of all levels from largest to smallest and return the one in position rank (counting from 1). Equal weights take separate positions.
If the tree has fewer than rank levels, return -1.root=tree_node([4, 7, 2, 1, None, 9, 3]), rank=2 → 9
The level totals are 4, then 7 + 2 = 9, then 1 + 9 + 3 = 13. Sorted from largest: 13, 9, 4, so second place goes to 9.
root=tree_node([6, 3, 3, None, 6]), rank=1 → 6
All three levels total 6, so first place is 6. Equal totals still count as separate places.
root=tree_node([2, 5, 1]), rank=3 → -1
There are three nodes but only two levels (2 and 5 + 1 = 6), so there is no third place: the answer is -1.
root=tree_node([4, 7, 2, 1, None, 9, 3]), rank=2
9
Run checks the first 3 tests in your browser. Submit checks all 46 tests on the server, hidden ones included.