Example
Input
Input
Output
Implementation
public static Tree mirror(Tree root){
if(root == null)
return null;
Tree leftTree = null, rightTree = null;
if(root.left != null)
leftTree = mirror(root.left);
if(root.right != null)
rightTree = mirror(root.right);
root.left = rightTree;
root.right = leftTree;
return root;
}
This is not mirroring right.The connections cannot be changed. For example B connected to D,E cannot be suddenly connected to D,F....
ReplyDelete