From 022fe8afccac97df3b5a071799e032a3f1b935f0 Mon Sep 17 00:00:00 2001 From: lixiandea Date: Wed, 11 Nov 2020 15:37:34 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90100.=20=E7=9B=B8=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E6=A0=91=E3=80=91=E3=80=90Python=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../二叉搜索树操作集锦.md | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/数据结构系列/二叉搜索树操作集锦.md b/数据结构系列/二叉搜索树操作集锦.md index 801b8fb..45b77df 100644 --- a/数据结构系列/二叉搜索树操作集锦.md +++ b/数据结构系列/二叉搜索树操作集锦.md @@ -310,4 +310,29 @@ void BST(TreeNode root, int target) {

-======其他语言代码====== \ No newline at end of file +======其他语言代码====== + +[lixiandea](https://github.com/lixiandea)提供第100题Python3代码: +```python3 +# 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: + if q == None: + return True + else: + return False + if q == None: + return False + # 当前节点相同且左子树和右子树分别相同 + return p.val==q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) +``` \ No newline at end of file