Swap Nodes in Binary tree of every k 'th level | Java program swap nodes ; A Binary Tree Node ; function to create a new tree node ; A utility function swap left - node & right node of tree of every k 'th level  ; base case ; if current level + 1 is present in swap vector then we swap left & right node ; Recur for left and right subtrees ; This function mainly calls recursive function swapEveryKLevelUtil ( ) ; call swapEveryKLevelUtil function with initial level as 1. ; Utility method for inorder tree traversal ; Driver Code ; 1 / \ 2 3 / / \ 4 7 8
class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void swapEveryKLevelUtil ( Node root , int level , int k ) { if ( root == null || ( root . left == null && root . right == null ) ) return ; if ( ( level + 1 ) % k == 0 ) { Node temp = root . left ; root . left = root . right ; root . right = temp ; } swapEveryKLevelUtil ( root . left , level + 1 , k ) ; swapEveryKLevelUtil ( root . right , level + 1 , k ) ; } static void swapEveryKLevel ( Node root , int k ) { swapEveryKLevelUtil ( root , 1 , k ) ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 8 ) ; root . right . left = newNode ( 7 ) ; int k = 2 ; System . out . println ( " Before ▁ swap ▁ node ▁ : " ) ; inorder ( root ) ; swapEveryKLevel ( root , k ) ; System . out . println ( " After swap Node : " );  inorder ( root ) ; } }