lhl
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
  • python

  • leetcode

    • 数组

    • 位运算

    • 动态规划

    • 链表

    • 栈

    • 树

      • 94.二叉树的中序遍历
      • 100.相同的树
      • 101.对称二叉树
      • 226.翻转二叉树
      • 543. 二叉树的直径
  • 软件测试

  • Git

  • linux

  • 产品

  • MySql

  • docker

  • leetcode
  • tree
2023-04-29

相同的树

https://leetcode-cn.com/problems/same-tree/ (opens new window) 需要注意的点

# 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 not p and not q:
            return True

        if (not p or not q) or p.val != q.val:
            return False

         

        def reverseTree(n1: TreeNode, n2: TreeNode)->bool:
            if not n1 or not n2:   
                return n1 == n2
            
            if n1.val != n2.val:
                return False
            
            return reverseTree(n1.left, n2.left) and reverseTree(n1.right, n2.right)

        
        return reverseTree(p, q)
94.二叉树的中序遍历
101.对称二叉树

← 94.二叉树的中序遍历 101.对称二叉树→

最近更新
01
lhl learn notes
02
filter
06-09
03
decorator
06-09
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式