5. Mirror of a Binary tree
5. Mirror of a Binary tree
Problem link :- click here
package Binary_Tree;
public class BinaryTree__5
{
static void inorder(Node root)
{
if(root == null)
return;
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
static Node mirror(Node root)
{
if(root == null)
return root;
Node node = new Node();
node.data = root.data;
node.left = mirror(root.right);
node.right = mirror(root.left);
return node;
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
int arr[] = {10, 20, 30, 40, 60};
tree.root = tree.createTree(arr, 0);
System.out.println("Inorder of the given tree : ");
inorder(tree.root);
Node root2 = mirror(tree.root);
System.out.println("\nInorder of the mirror tree : ");
inorder(root2);
}
}
Time complexity :- O(n)
Space complexity :- O(n)
Comments
Post a Comment