import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root1 TreeNode类
     * @param root2 TreeNode类
     * @return bool布尔型
     */
    public boolean isContains (TreeNode root1, TreeNode root2) {
        // write code here
        boolean rs = true;
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode tmpNode = root1;
        queue.add(tmpNode);
        // 在 t1 树中,找到一棵以 root2 为节点的子树
        while (!queue.isEmpty()) {
            tmpNode = queue.poll();
            if (tmpNode.val == root2.val) {
                break;
            }
            if (null != tmpNode.left) {
                queue.add(tmpNode.left);
            }
            if (null != tmpNode.right) {
                queue.add(tmpNode.right);
            }
        }
        if (tmpNode.val != root2.val) {
            return false;
        }
        return process(tmpNode, root2);
    }

    public boolean process(TreeNode node1, TreeNode node2) {
        if (null == node1 && null == node2) {
            return true;
        }
        if ((null == node1 && null != node2) || (null != node1 && null == node2)) {
            return false;
        }
        if (node1.val != node2.val) {
            return false;
        }
        boolean left = process(node1.left, node2.left);
        boolean right = process(node1.right, node2.right);
        return node1.val == node2.val && left && right;
    }
}