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.

 

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution(object):
 9     def isSameTree(self, p, q):
10         """
11         :type p: TreeNode
12         :type q: TreeNode
13         :rtype: bool
14         """
15         if p and q:
16             return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
17         return p is q

is检查两个对象是否是同一个对象,而==检查他们是否相等

posted on 2017-03-15 15:42  Ci_pea  阅读(94)  评论(0编辑  收藏  举报