7ca7c14553
It's possible to write this with a few less twisty special cases. Tested manually with a randomly-distributed input tree as well as manually trying to hit special cases around first/last entries. Signed-off-by: Daniel Stone <daniels@collabora.com>