Close

Mirror image of a binary tree

Given a binary tree, how to convert it to it’s mirror image?For example consider the following binary tree and it’s mirror image. Here simply, the left and right sub trees of each node are inverted. So we have to simply invert the given binary tree. It is a simple implementation problem. Using recursion we can…

Creating a balanced binary search tree from sorted array

Given a sorted array, how do we create binary search which height balanced. For example the array [1,2,3,4,5] must be transformed to the following binary tree                            3                /                 2    4              /                     1        5 We can use the divide and conquer approach to solve this problem. To create a…

Checking if a binary tree is balanced

Given a binary tree, how do we check if it is balanced or not? We say that a binary tree is balanced if height difference of left  and the right sub-trees is at most 1 at  any node. Consider the following examples   1          1               1             1  /                       /              …

Right view of the binary tree

Given a binary tree, how do we print the right view of it?For example, consider the simple binary tree below    1 / 2   3 The right view of this is 1, 3. Similarly consider one more example             1          /           2    5        /          3   4  Right view for this tree contains…