To get mirror image of a binary tree
Page 1 of 1
To get mirror image of a binary tree
pubic static void getmirror(TreeNode root){
if(root==null)
return;
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
getmirror(root.left);
getmirror(root.right;)
}
if(root==null)
return;
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
getmirror(root.left);
getmirror(root.right;)
}
Giving two Binary Tree, judeg whether they are mirror?
public static boolean judgeMirror(TreeNode n1, TreeNode n2){
if(n1!=n2)
return false;
if(n1==null&&n2==null)
return true;
judgeMirror(n1.left,n2.right);
judgeMirror(n1.right,n2.left);
return true;
}
if(n1!=n2)
return false;
if(n1==null&&n2==null)
return true;
judgeMirror(n1.left,n2.right);
judgeMirror(n1.right,n2.left);
return true;
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|