首页 > 代码库 > #Leet Code# Same Tree

#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 node10     # @param q, a tree node11     # @return a boolean12     def isSameTree(self, p, q):13         if (p is None and q is None):14             return True15         elif (p is None and q is not None) or (p is not None and q is None):16             return False17         else:18             if (p.val != q.val):19                 return False20             else:21                 return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)