-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-Same Tree.py
More file actions
30 lines (29 loc) · 769 Bytes
/
100-Same Tree.py
File metadata and controls
30 lines (29 loc) · 769 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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p and not q:
return True
if not p or not q:
return False
return self.bfs(p) == self.bfs(q)
def bfs(self, a):
rtn = [a.val]
if a.left:
rtn += self.bfs(a.left)
else:
rtn.append(None)
if a.right:
rtn += self.bfs(a.right)
else:
rtn.append(None)
return rtn