【LEETCODE】100-Same Tree

来源:互联网 发布:nginx配置静态页面 编辑:程序博客网 时间:2024/05/19 12:37

Same Tree


Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.


# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def isSameTree(self, p, q):        """        :type p: TreeNode        :type q: TreeNode        :rtype: bool        """                if p == q == None:                                     #同为空时,True            return True        #elif not (p and q) or p.val != q.val:        elif p==None or q==None or p.val != q.val:             # 其中一个为空,或二者值不相等时,False            return False        else:            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)           #递归比较根的左右子树


0 0