【leetcode】100:相同的树

 

这个题目直接递归就行,难度不大,就是要考虑全面一些哦,这道题应该也是剑指offer的原题 ,复习一下,挺简单的,代码如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if p==None and q==None:
            return True
        if p==None and q!=None or p!=None and q==None:
            return False
        
        bool_one=self.isSameTree(p.left,q.left)
        bool_two=self.isSameTree(p.right,q.right)
        if p.val==q.val:
            bool_three=True
        else:
            bool_three=False

        return bool_one and bool_two and bool_three

速度如下:
【leetcode】100:相同的树

 

相关文章:

  • 2021-06-03
  • 2022-02-26
  • 2021-05-13
  • 2021-05-15
  • 2021-11-29
  • 2021-11-27
猜你喜欢
  • 2021-11-20
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
相关资源
相似解决方案