Skip to main content

Posts

Showing posts with the label The Crazy Programmer Invert a Binary Tree – Recursive and Iterative Approach in Java Invert a Binary Tree – Recursive and Iterative Approach in Java The Crazy Programmer

Invert a Binary Tree – Recursive and Iterative Approach in Java Vijay Sinha The Crazy Programmer

In this article we have a look at another interesting problem related to binary tree . Given a binary tree we have to invert the tree and print it. We discuss different approaches to solve this problem along with their time and space complexities The inversion of a binary tree or the invert of a binary tree means to convert the tree into it’s Mirror image. In simple terms, it is a tree whose left children and right children of all non-leaf nodes are swapped. Note: The leaf nodes will also get interchanged. It is recommended to learn In-Order and Post Order traversal before proceeding with this problem. Let us look at an example of a binary tree:   In the above figure we see the sample tree and it’s inverted or mirror version. Input: 20 / \ / \ 10 30 / \ / \ / \ / \ 5