Binary search tree serialization linear time