public class CheckMirroImageInTree {
public static void main(String[] args) {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left =new Node(6);
node.right.right = new Node(7);
Node node2 = new Node(1);
node2.left = new Node(3);
node2.right = new Node(2);
node2.left.left = new Node(7);
node2.left.right = new Node(6);
node2.right.left =new Node(5);
node2.right.right = new Node(4);
System.out.println(checkMirror(node, node2));
}
static boolean checkMirror(Node node1,Node node2) {
if(node1==null && node2 == null) {
return true;
}
if(node1==null || node2==null) {
return false;
}
if(node1.value == node2.value) {
if(checkMirror(node1.left, node2.right) && checkMirror(node1.right, node2.left)) {
return true;
}
}
return false;
}
static class Node{
Integer value;
Node left;
Node right;
Node(Integer value){
this.value = value;
left = right = null;
}
}
}
No comments:
Post a Comment