import java.util.*;

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}*/
public class IdenticalTree {
    public boolean chkIdentical(TreeNode A, TreeNode B) {
        // write code here
        if(A == null && B != null){
            return false;
        }
        if(isSame(A, B)){
            return true;
        }
        if(chkIdentical(A.left, B)){
            return true;
        }else if(chkIdentical(A.right, B)){
            return true;
        }
        return false;
    }

    public boolean isSame(TreeNode A, TreeNode B){
        if(A == null && B == null){
            return true;
        }
        if(A == null || B == null){
            return false;
        }
        return A.val == B.val && isSame(A.left, B.left) && isSame(A.right, B.right);
    }
}