-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_same_tree.py
More file actions
48 lines (34 loc) · 998 Bytes
/
is_same_tree.py
File metadata and controls
48 lines (34 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import Optional
# https://leetcode.com/problems/same-tree/
# 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
tq = TreeNode(1)
tq.left = TreeNode(2)
tq.right = TreeNode(3)
tp = TreeNode(1)
tp.left = TreeNode(2)
tp.right = TreeNode(4)
# traverse down each tree simultaneously using the same order
# if at anypoint the current node values are not the same then set
# value to False
def isSameTree(p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
is_same = True
def dfs(p, q):
nonlocal is_same
if (not p and q) or (p and not q):
is_same = False
return
if not p and not q:
return
if p.val != q.val:
is_same = False
return
dfs(p.left, q.left)
dfs(p.right, q.right)
dfs(p, q)
return is_same
print(isSameTree(tq, tp))