【100. 相同的树】【Python】

This commit is contained in:
lixiandea
2020-11-11 15:37:34 +08:00
parent 8b8f413585
commit 022fe8afcc

View File

@ -310,4 +310,29 @@ void BST(TreeNode root, int target) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
======其他语言代码======
[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)
```