import java.util.*;
public class Main{
public static void main(String [] args)
{
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();
System.out.println(tree1.StructurelyIdentical(tree2));
}
//Binary Tree
private static class BinaryTree
{
//Node
private class Node{
int data;
Node left;
Node right;
Node(int data, Node left, Node right)
{
this.data = data;
this.left = left;
this.right = right;
}
}
private Node root;
//BinaryTree construction
BinaryTree()
{
Scanner s = new Scanner(System.in);
this.root = construct(s, null, false);
}
private Node construct(Scanner s, Node Parent, Boolean ilc)
{
nodedata = s.nextInt();
Node node = new Node(nodedata, null, null);
boolean choice = s.nextBoolean();
if(choice)
node.left = construct(s, node, true);
choice = s.nextBoolean();
if(choice)
node.right = construct(s, node, false);
return node;
}
public boolean StructurelyIdentical(BinaryTree other)
{
return this.StructurelyIdentical(this.root, other.root);
}
private boolean StructurelyIdentical(Node node1, Node node2)
{
if((node1 != null && node2 == null) || (node1 == null && node2 != null))
return false;
if(node1 == null && node2 == null)
return true;
if(node1 != null && node2 != null)
{
if(!StructurelyIdentical(node1.left, node2.left))
return false;
if(!StructurelyIdentical(node1.right, node2.right))
return false;
}
return true;
}
}
}