mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 20:06:31 +08:00
【100. 相同的树】【Python】
This commit is contained in:
@ -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)
|
||||
```
|
Reference in New Issue
Block a user