Skip to content

Commit 681cef5

Browse files
committed
is the same tree
1 parent 799fcc8 commit 681cef5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

SameTree.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding: utf-8
2+
__author__ = 'devin'
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.left = None
9+
# self.right = None
10+
11+
class Solution(object):
12+
def isSameTree(self, p, q):
13+
"""
14+
:type p: TreeNode
15+
:type q: TreeNode
16+
:rtype: bool
17+
"""
18+
if p is None and q is None:
19+
return True
20+
21+
if p is not None and q is not None and p.val == q.val:
22+
left_tree = self.isSameTree(p.left, q.left)
23+
right_tree = self.isSameTree(p.right, q.right)
24+
if left_tree and right_tree:
25+
return True
26+
else:
27+
return False
28+
else:
29+
return False

0 commit comments

Comments
 (0)