Я написал код, чтобы проверить, является ли дерево симметричным (дерево симметрично, если оно является зеркальным отражением самого себя) или нет. Допустим, нам дано дерево A. Здесь я пытаюсь создать новое дерево B, используя функцию mirror (), которая является зеркальным отображением дерева A. Когда я печатаю дерево B в основной функции, оно правильнопечать зеркального отображения. Но когда та же зеркальная функция вызывается внутри функции isSymmetric (), она возвращает само исходное дерево. Другие решения уже доступны на разных платформах. Я просто хочу знать, почему мой код ведет себя по-разному, когда одна и та же функция вызывается в разных местах.
import java.util. *;
class BinaryTree {Корень узла;
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));
}
}