-
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 (27 loc) · 878 Bytes
/
100_same_tree.py
File metadata and controls
30 lines (27 loc) · 878 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
'''
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
'''
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x, left, right):
self.val = x
self.left = left
self.right = right
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if q == None and p == None:
return True
if q == None or p == None or q.val != p.val:
return False
return self.isSameTree(q.left, p.left) and self.isSameTree(q.right, p.right)
s = Solution()
n1 = TreeNode(1, None, None)
n2 = TreeNode(1, n1, None)
assert(s.isSameTree(n1, n1))
assert(not s.isSameTree(n2, n1))