提交 022fe8af 编写于 作者: L lixiandea

【100. 相同的树】【Python】

上级 8b8f4135
......@@ -310,4 +310,29 @@ void BST(TreeNode root, int target) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
\ 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册