Skip to content

Commit c1a0500

Browse files
committed
feat: adds same_tree exercise
1 parent aef9fbb commit c1a0500

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

same_tree.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Optional
2+
3+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
14+
if not p and not q:
15+
return True
16+
17+
if not p or not q:
18+
return False
19+
20+
if p.val != q.val:
21+
return False
22+
23+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

0 commit comments

Comments
 (0)