kd-tree, not sure if the result is correct though.