#Leet Code# Same Tree

语言:Python

描述:使用递归实现

 

 1 # Definition for a  binary tree node
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution:
 9     # @param p, a tree node
10     # @param q, a tree node
11     # @return a boolean
12     def isSameTree(self, p, q):
13         if (p is None and q is None):
14             return True
15         elif (p is None and q is not None) or (p is not None and q is None):
16             return False
17         else:
18             if (p.val != q.val):
19                 return False
20             else:
21                 return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

 

 

posted @ 2014-07-07 17:30  mess4u  阅读(170)  评论(0编辑  收藏  举报