Given a binary tree, the task is to flip the binary tree towards right direction that is clockwise. See below examples to see the transformation.

In the flip operation, left most node becomes the root of flipped tree and its parent become its right child and the right sibling become its left child and same should be done for all left most nodes recursively.





Below is main rotation code of a subtree

root->left->left = root->right; root->left->right = root; root->left = NULL; root->right = NULL;

The above code can be understood by following diagram –



as we are storing root->left in flipped root, flipped subtree gets stored in each recursive call.

C++ filter_none edit

close play_arrow link

brightness_4

code #include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; }; struct Node* newNode( int data) { struct Node *temp = new struct Node; temp->data = data; temp->left = temp->right = NULL; return temp; } Node* flipBinaryTree(Node* root) { if (root == NULL) return root; if (root->left == NULL && root->right == NULL) return root; Node* flippedRoot = flipBinaryTree(root->left); root->left->left = root->right; root->left->right = root; root->left = root->right = NULL; return flippedRoot; } void printLevelOrder(Node *root) { if (root == NULL) return ; queue<Node *> q; q.push(root); while (1) { int nodeCount = q.size(); if (nodeCount == 0) break ; while (nodeCount > 0) { Node *node = q.front(); cout << node->data << " " ; q.pop(); if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); nodeCount--; } cout << endl; } } int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->right->left = newNode(4); root->right->right = newNode(5); cout << "Level order traversal of given tree

" ; printLevelOrder(root); root = flipBinaryTree(root); cout << "

Level order traversal of the flipped" " tree

" ; printLevelOrder(root); return 0; } chevron_right filter_none Java filter_none edit

close play_arrow link

brightness_4

code import java.util.Queue; import java.util.LinkedList; public class FlipTree { public static Node flipBinaryTree(Node root) { if (root == null ) return root; if (root.left == null && root.right == null ) return root; Node flippedRoot=flipBinaryTree(root.left); root.left.left=root.right; root.left.right=root; root.left=root.right= null ; return flippedRoot; } public static void printLevelOrder(Node root) { if (root== null ) return ; Queue<Node> q= new LinkedList<>(); q.add(root); while ( true ) { int nodeCount = q.size(); if (nodeCount == 0 ) break ; while (nodeCount > 0 ) { Node node = q.remove(); System.out.print(node.data+ " " ); if (node.left != null ) q.add(node.left); if (node.right != null ) q.add(node.right); nodeCount--; } System.out.println(); } } public static void main(String args[]) { Node root= new Node( 1 ); root.left= new Node( 2 ); root.right= new Node( 1 ); root.right.left = new Node( 4 ); root.right.right = new Node( 5 ); System.out.println( "Level order traversal of given tree" ); printLevelOrder(root); root = flipBinaryTree(root); System.out.println( "Level order traversal of flipped tree" ); printLevelOrder(root); } } class Node { int data; Node left, right; Node( int data) { this .data=data; } }; chevron_right filter_none Python filter_none edit

close play_arrow link

brightness_4

code class Node: def __init__( self , data): self .data = data self .right = None self .left = None def flipBinaryTree(root): if root is None : return root if root.left is None and root.right is None : return root flippedRoot = flipBinaryTree(root.left) root.left.left = root.right root.left.right = root root.left = root.right = None return flippedRoot def printLevelOrder(root): if root is None : return from Queue import Queue q = Queue() q.put(root) while ( True ): nodeCount = q.qsize() if nodeCount = = 0 : break while nodeCount > 0 : node = q.get() print node.data, if node.left is not None : q.put(node.left) if node.right is not None : q.put(node.right) nodeCount - = 1 print root = Node( 1 ) root.left = Node( 2 ) root.right = Node( 3 ) root.right.left = Node( 4 ) root.right.right = Node( 5 ) print "Level order traversal of given tree" printLevelOrder(root) root = flipBinaryTree(root) print "

Level order traversal of the flipped tree" printLevelOrder(root) chevron_right filter_none C# filter_none edit

close play_arrow link

brightness_4

code using System; using System.Collections.Generic; public class FlipTree { public static Node flipBinaryTree(Node root) { if (root == null ) return root; if (root.left == null && root.right == null ) return root; Node flippedRoot = flipBinaryTree(root.left); root.left.left = root.right; root.left.right = root; root.left = root.right = null ; return flippedRoot; } public static void printLevelOrder(Node root) { if (root == null ) return ; Queue<Node> q = new Queue<Node>(); q.Enqueue(root); while ( true ) { int nodeCount = q.Count; if (nodeCount == 0) break ; while (nodeCount > 0) { Node node = q.Dequeue(); Console.Write(node.data+ " " ); if (node.left != null ) q.Enqueue(node.left); if (node.right != null ) q.Enqueue(node.right); nodeCount--; } Console.WriteLine(); } } public static void Main(String []args) { Node root = new Node(1); root.left = new Node(2); root.right = new Node(1); root.right.left = new Node(4); root.right.right = new Node(5); Console.WriteLine( "Level order traversal of given tree" ); printLevelOrder(root); root = flipBinaryTree(root); Console.WriteLine( "Level order traversal of flipped tree" ); printLevelOrder(root); } } public class Node { public int data; public Node left, right; public Node( int data) { this .data = data; } }; chevron_right filter_none

Level order traversal of given tree 1 2 3 4 5 Level order traversal of the flipped tree 2 3 1 4 5

Iterative Approach

This approach is contributed by Pal13.

The iterative solution follows the same approach as the recursive one, the only thing we need to pay attention to is to save the node information that will be overwritten.



C++ filter_none edit

close play_arrow link

brightness_4

code #include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; }; struct Node* newNode( int data) { struct Node *temp = new struct Node; temp->data = data; temp->left = temp->right = NULL; return temp; } Node* flipBinaryTree(Node* root) { Node *curr = root; Node *next = NULL; Node *temp = NULL; Node *prev = NULL; while (curr) { next = curr->left; curr->left = temp; temp = curr->right; curr->right = prev; prev = curr; curr = next; } return prev; } void printLevelOrder(Node *root) { if (root == NULL) return ; queue<Node *> q; q.push(root); while (1) { int nodeCount = q.size(); if (nodeCount == 0) break ; while (nodeCount > 0) { Node *node = q.front(); cout << node->data << " " ; q.pop(); if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); nodeCount--; } cout << endl; } } int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->right->left = newNode(4); root->right->right = newNode(5); cout << "Level order traversal of given tree

" ; printLevelOrder(root); root = flipBinaryTree(root); cout << "

Level order traversal of the flipped" " tree

" ; printLevelOrder(root); return 0; } chevron_right filter_none Java filter_none edit

close play_arrow link

brightness_4

code import java.util.*; 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 Node flipBinaryTree(Node root) { Node curr = root; Node next = null ; Node temp = null ; Node prev = null ; while (curr != null ) { next = curr.left; curr.left = temp; temp = curr.right; curr.right = prev; prev = curr; curr = next; } return prev; } static void printLevelOrder(Node root) { if (root == null ) return ; Queue<Node> q = new LinkedList<Node>(); q.add(root); while ( true ) { int nodeCount = q.size(); if (nodeCount == 0 ) break ; while (nodeCount > 0 ) { Node node = q.peek(); System.out.print(node.data + " " ); q.remove(); if (node.left != null ) q.add(node.left); if (node.right != null ) q.add(node.right); nodeCount--; } System.out.println(); } } public static void main(String args[]) { Node root = newNode( 1 ); root.left = newNode( 2 ); root.right = newNode( 3 ); root.right.left = newNode( 4 ); root.right.right = newNode( 5 ); System.out.print( "Level order traversal " + "of given tree

" ); printLevelOrder(root); root = flipBinaryTree(root); System.out.print( "

Level order traversal " + "of the flipped tree

" ); printLevelOrder(root); } } chevron_right filter_none C# filter_none edit

close play_arrow link

brightness_4

code using System; using System.Collections.Generic; class GFG { public class Node { public int data; public Node left, right; } public static Node newNode( int data) { Node temp = new Node(); temp.data = data; temp.left = temp.right = null ; return temp; } public static Node flipBinaryTree(Node root) { Node curr = root; Node next = null ; Node temp = null ; Node prev = null ; while (curr != null ) { next = curr.left; curr.left = temp; temp = curr.right; curr.right = prev; prev = curr; curr = next; } return prev; } public static void printLevelOrder(Node root) { if (root == null ) { return ; } LinkedList<Node> q = new LinkedList<Node>(); q.AddLast(root); while ( true ) { int nodeCount = q.Count; if (nodeCount == 0) { break ; } while (nodeCount > 0) { Node node = q.First.Value; Console.Write(node.data + " " ); q.RemoveFirst(); if (node.left != null ) { q.AddLast(node.left); } if (node.right != null ) { q.AddLast(node.right); } nodeCount--; } Console.WriteLine(); } } public static void Main( string [] args) { Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.right.left = newNode(4); root.right.right = newNode(5); Console.Write( "Level order traversal " + "of given tree

" ); printLevelOrder(root); root = flipBinaryTree(root); Console.Write( "

Level order traversal " + "of the flipped tree

" ); printLevelOrder(root); } } chevron_right filter_none

Level order traversal of given tree 1 2 3 4 5 Level order traversal of the flipped tree 2 3 1 4 5

Complexity Analysis:

Time complexity: O(n) as in the worst case, depth of binary tree will be n.

Auxiliary Space : O(1).

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.