Binary tree construction, merged sorted array and next permutation