Leetcode 100 Same Tree python

题目:

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 class Solution(object):
 2     def isSameTree(self, p, q):
 3         if p == None and q == None:
 4             return True
 5         elif p == None and q != None:
 6             return False
 7         elif p != None and q == None:
 8             return False
 9         elif p.val != q.val:
10             return False
11         elif p.val == q.val:
12             return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

 

posted on 2016-04-16 14:19  def_sysu  阅读(344)  评论(0)    收藏  举报

导航