root of a binary tree and an integer limit. A leaf is a node with no children. The distance between two nodes is the number of edges on the unique path between them.
Count the unordered pairs of distinct leaves whose distance is at most limit.root=tree_node([5, 8, 2, 4, None, 9, 1]), limit=3 → 1
The leaves are 4, 9 and 1. The path between 9 and 1 has 2 edges, while 4 is 4 edges away from both 9 and 1. One pair qualifies.
root=tree_node([5, 8, 2, 4, None, 9, 1]), limit=4 → 3
The same tree, but now paths of up to 4 edges are allowed, so all three pairs of leaves qualify.
root=tree_node([7]), limit=1 → 0
Node.val ≤ 100limit ≤ 10root=tree_node([5, 8, 2, 4, None, 9, 1]), limit=3
1
Run checks the first 3 tests in your browser. Submit checks all 67 tests on the server, hidden ones included.