题目
代码
执行用时:100 ms, 在所有 Python3 提交中击败了68.84% 的用户
内存消耗:19.5 MB, 在所有 Python3 提交中击败了24.57% 的用户
通过测试用例:48 / 48
class Solution:
def judge(self,A,B):
if (A and not B) or (not A and not B):
return True
if (not A and B) or (A.val!=B.val):
return False
return self.judge(A.left,B.left) and self.judge(A.right,B.right)
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
if not A or not B:
return False
return self.judge(A,B) or self.isSubStructure(A.left,B) or self.isSubStructure(A.right,B)

本文介绍了一种使用Python实现的递归方法,用于判断两个二叉树的子结构关系。通过`judge`和`isSubStructure`函数,解决了一个在给定树结构中查找子结构的问题。代码高效,分别在执行时间和内存消耗上击败了大部分用户。
172万+

被折叠的 条评论
为什么被折叠?



