Input: N = 3, leaf[] = {4, 3, 2, 1, 5, 6, 7, 8}
Tree representing given input
Output: 3
Explanation: Swap the left and right part of subtree having leaf nodes 4 and 3, swap left and right part of subtree having leaf nodes 2 and 1 and finally swap left and right part of whole left subtree of root having leaf nodes as 4, 3, 2, 1. Total swap operations performed is equal to three which is minimum to sort the leaf nodes in increasing order.
Input: N=2, leaf[] = {3, 1, 4, 2}
Tree representing given input
Output: -1
Explanation: We can perform two swapping operations on left and right part of subtrees having leaf nodes as 3&1 and 4&2, but we will get the leaf nodes in order {1, 3, 2, 4} which isn't sorted. Since the leaf nodes are not sorted in increasing order so we return -1.