Java 查找树是否对称的代码中的错误
我编写了一个代码来检查一棵树是否对称(如果它是自身的镜像,那么它就是对称的)。比方说,我们得到了一棵树a。在这里,我尝试使用函数mirror()创建一棵新树B,这是树a的镜像。当我在main函数中打印树B时,它正确地打印了镜像。但是,当在函数issymmetry()内调用相同的镜像函数时,它将返回原始树本身。其他解决方案已经在不同的平台上提供。我只是想知道,当同一个函数在不同的地方被调用时,为什么我的代码会有不同的行为 导入java.util.* 类二叉树 { 节根Java 查找树是否对称的代码中的错误,java,binary-tree,Java,Binary Tree,我编写了一个代码来检查一棵树是否对称(如果它是自身的镜像,那么它就是对称的)。比方说,我们得到了一棵树a。在这里,我尝试使用函数mirror()创建一棵新树B,这是树a的镜像。当我在main函数中打印树B时,它正确地打印了镜像。但是,当在函数issymmetry()内调用相同的镜像函数时,它将返回原始树本身。其他解决方案已经在不同的平台上提供。我只是想知道,当同一个函数在不同的地方被调用时,为什么我的代码会有不同的行为 导入java.util.* 类二叉树 { 节根 BinaryTree(int
BinaryTree(int d) {
root = new Node(d);
}
BinaryTree() {
root = null;
}
static class Node {//why static is needed ?
int data;
Node left, right;
Node(int d) {
data = d;
left = null;
right = null;
}
}
boolean isSymmetric(Node root) {
if(root == null)
return true;
if(root.left == null && root.right == null)
return true;
Node root2 = mirror(root);
printTree(root);
System.out.println();
printTree(root2);//this should print mirror image of tree but it is printing the original tree
return isMirror(root, root2);
}
Node mirror(Node root) {
if(root == null)
return root;
if(root.left == null && root.right == null) {
return root;
}
Node left = mirror(root.left);
Node right = mirror(root.right);
root.left = right;
root.right = left;
return root;
}
boolean isMirror(Node n1, Node n2) {
if(n1 == null && n2 == null)
return true;
if(n1 == null || n2 == null)
return false;
if(n1.data != n2.data)
return false;
return isMirror(n1.left, n2.left) && isMirror(n1.right, n2.right);
}
void printTree(Node root) {
Queue<Node> q = new LinkedList<Node>();
if(root != null) {
q.add(root);
}
else {
System.out.println("empty tree");
}
while(!q.isEmpty()) {
Node y = q.remove();
System.out.print(y.data + " ");
if(y.left != null)
q.add(y.left);
if(y.right != null)
q.add(y.right);
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BinaryTree t = new BinaryTree(5);
t.root.left = new Node(6);
t.root.right = new Node(4);
t.root.left.left = new Node(7);
t.root.left.right = new Node(8);
t.root.right.left = new Node(9);
t.root.right.right = new Node(10);
t.printTree(t.root);
t.root = t.mirror(t.root);
System.out.println();
t.printTree(t.root);
System.out.println();
System.out.print(t.isSymmetric(t.root));
}
BinaryTree(intd){
根=新节点(d);
}
二叉树(){
root=null;
}
静态类节点{//为什么需要静态?
int数据;
左、右淋巴结;
节点(int d){
数据=d;
左=空;
右=空;
}
}
布尔isSymmetric(节点根){
if(root==null)
返回true;
if(root.left==null&&root.right==null)
返回true;
节点root2=镜像(根);
打印树(根);
System.out.println();
printTree(root2);//这应该打印树的镜像,但它正在打印原始树
返回isMirror(根,根2);
}
节点镜像(节点根){
if(root==null)
返回根;
if(root.left==null&&root.right==null){
返回根;
}
节点左=镜像(root.left);
节点右侧=镜像(root.right);
root.left=右;
root.right=左;
返回根;
}
布尔isMirror(节点n1,节点n2){
如果(n1==null&&n2==null)
返回true;
如果(n1==null | | n2==null)
返回false;
如果(n1.数据!=n2.数据)
返回false;
返回isMirror(n1.左,n2.左)和&isMirror(n1.右,n2.右);
}
无效打印树(节点根){
队列q=新的LinkedList();
if(root!=null){
q、 添加(根);
}
否则{
System.out.println(“空树”);
}
而(!q.isEmpty()){
节点y=q.remove();
系统输出打印(y.data+“”);
如果(y.左!=null)
q、 添加(y.左);
如果(y.right!=null)
q、 加(y.右);
}
}
公共静态void main(字符串[]args)引发java.lang.Exception
{
//你的密码在这里
BinaryTree t=新的BinaryTree(5);
t、 root.left=新节点(6);
t、 root.right=新节点(4);
t、 root.left.left=新节点(7);
t、 root.left.right=新节点(8);
t、 root.right.left=新节点(9);
t、 root.right.right=新节点(10);
t、 printree(t.root);
t、 root=t.mirror(t.root);
System.out.println();
t、 printree(t.root);
System.out.println();
系统输出打印(t.isSymmetric(t.root));
}
}主要原因是在同一根上调用镜像方法两次。第二次调用与第一次调用的效果相反 你可以改变你的代码来达到这样的效果,你的isMirror函数有一个错误,我修正了
public class BinaryTree {
Node root;
BinaryTree(int d) {
root = new Node(d);
}
BinaryTree() {
root = null;
}
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
BinaryTree t = new BinaryTree(5);
t.root.left = new Node(6);
t.root.right = new Node(4);
t.root.left.left = new Node(7);
t.root.left.right = new Node(8);
t.root.right.left = new Node(9);
t.root.right.right = new Node(10);
t.printTree(t.root);
Node MirroredNode = t.mirror(t.root.clone());
System.out.println();
t.printTree(t.root);
t.printTree(MirroredNode);
System.out.println();
System.out.print(t.isSymmetric(t.root));
}
boolean isSymmetric(Node root) {
if (root == null)
return true;
if (root.left == null && root.right == null)
return true;
Node root2 = mirror(root.clone());
printTree(root);
System.out.println();
printTree(root2);//this should print mirror image of tree but it is printing the original tree
return isMirror(root, root2);
}
Node mirror(Node root) {
if (root == null)
return root;
if (root.left == null && root.right == null) {
return root;
}
Node left = mirror(root.left.clone());
Node right = mirror(root.right.clone());
root.left = right;
root.right = left;
return root;
}
boolean isMirror(Node n1, Node n2) {
if (n1 == null && n2 == null)
return true;
if (n1 == null || n2 == null)
return false;
if (n1.data != n2.data)
return false;
return isMirror(n1.left, n2.right) && isMirror(n1.right, n2.left);
}
void printTree(Node root) {
Queue<Node> q = new LinkedList<Node>();
if (root != null) {
q.add(root);
} else {
System.out.println("empty tree");
}
while (!q.isEmpty()) {
Node y = q.remove();
System.out.print(y.data + " ");
if (y.left != null)
q.add(y.left);
if (y.right != null)
q.add(y.right);
}
}
static class Node implements Cloneable {//why static is needed ?
int data;
Node left, right;
Node(int d) {
data = d;
left = null;
right = null;
}
@Override
public Node clone() {
try {
return (Node) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}
}
公共类二进制树{
节根;
二进制树(intd){
根=新节点(d);
}
二叉树(){
root=null;
}
公共静态void main(字符串[]args)引发java.lang.Exception{
//你的密码在这里
BinaryTree t=新的BinaryTree(5);
t、 root.left=新节点(6);
t、 root.right=新节点(4);
t、 root.left.left=新节点(7);
t、 root.left.right=新节点(8);
t、 root.right.left=新节点(9);
t、 root.right.right=新节点(10);
t、 printree(t.root);
Node MirroredNode=t.mirror(t.root.clone());
System.out.println();
t、 printree(t.root);
t、 打印树(镜像节点);
System.out.println();
系统输出打印(t.isSymmetric(t.root));
}
布尔isSymmetric(节点根){
if(root==null)
返回true;
if(root.left==null&&root.right==null)
返回true;
节点root2=mirror(root.clone());
打印树(根);
System.out.println();
printTree(root2);//这应该打印树的镜像,但它正在打印原始树
返回isMirror(根,根2);
}
节点镜像(节点根){
if(root==null)
返回根;
if(root.left==null&&root.right==null){
返回根;
}
Node left=mirror(root.left.clone());
Node right=mirror(root.right.clone());
root.left=右;
root.right=左;
返回根;
}
布尔isMirror(节点n1,节点n2){
如果(n1==null&&n2==null)
返回true;
如果(n1==null | | n2==null)
返回false;
如果(n1.数据!=n2.数据)
返回false;
返回isMirror(n1.左,n2.右)和&isMirror(n1.右,n2.左);
}
无效打印树(节点根){
队列q=新的LinkedList();
if(root!=null){
q、 添加(根);
}否则{
System.out.println(“空树”);
}
而(!q.isEmpty()){
节点y=q.remove();
系统输出打印(y.data+“”);
如果(y.左!=null)
q、 添加(y.左);
如果(y.right!=null)
q、 加(y.右);
}
}
静态类节点实现可克隆{//为什么需要静态?
int数据;
左、右淋巴结;
节点(int d){
数据=d;
左=空;
右=空;
}
@凌驾
公共节点克隆(){
试一试{
返回(节点)super.clone();
}捕获(CloneNotSupportedException e){
e、 printStackTrace();
}
返回null;
}
}
}
在isMirror
中,这两个节点相对于左侧和右侧子节点进行镜像
return isMirror(n1.left, n2.right) && isMirror(n1.right, n2.left);
static boolean isMirror(Node n1, Node n2)
boolean isSymmetric(Node root) { ...
return isMirror(root, root);