Skip to content

Commit 6bb053c

Browse files
committedJan 5, 2021
feat: same-tree
1 parent cbe2842 commit 6bb053c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎tree.same-tree.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
"""
11+
100. 相同的树
12+
https://leetcode-cn.com/problems/same-tree/
13+
给定两个二叉树,编写一个函数来检验它们是否相同。
14+
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
15+
"""
16+
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
17+
if not p and not q:
18+
return True
19+
20+
if not p or not q:
21+
return False
22+
23+
if p.val != q.val:
24+
return False
25+
26+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

0 commit comments

Comments
 (0)
Please sign in to comment.