首页 > 代码库 > #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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。