Ensure tree nodes are in correct order