"""
给定两个二叉树，编写一个函数来检验它们是否相同。

如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。

示例 1:

输入:       1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

输出: true
示例 2:

输入:      1          1
          /           \
         2             2

        [1,2],     [1,null,2]

输出: false
示例 3:

输入:       1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

输出: false

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/same-tree
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""
from typing import *


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def frontDFS(self, root: TreeNode):
        if not root:
            return [None]
        rtn = [root.val]
        rtn.extend(self.frontDFS(root.left))
        rtn.extend(self.frontDFS(root.right))
        return rtn

    def midDFS(self, root: TreeNode):
        if not root:
            return [None]
        rtn = []
        rtn.extend(self.frontDFS(root.left))
        rtn.append(root.val)
        rtn.extend(self.frontDFS(root.right))
        return rtn

    def cmpList(self, a, b):
        if len(a) != len(b):
            return False
        for i in range(len(a)):
            if a[i] != b[i]:
                return False
        return True

    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        frontDFSp = self.frontDFS(p)
        midDFSp = self.midDFS(p)
        # print(frontDFSp, midDFSp)

        frontDFSq = self.frontDFS(q)
        midDFSq = self.midDFS(q)
        # print(frontDFSq, midDFSq)

        return self.cmpList(frontDFSp, frontDFSq) and self.cmpList(midDFSp, midDFSq)
