-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisSameTree(100)
54 lines (43 loc) · 1.53 KB
/
isSameTree(100)
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
49
50
51
52
53
54
# Definition for a binary tree node.
class node:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def leafSimilar(self, root, root1):
myList = []
def dfs(node):
if not node:
myList.append("Null")
return None
print(None)
if node:
myList.append(node.val)
dfs(node.left)
dfs(node.right)
# below means if not node on both left and right
# print the recursive number above them(ie: 2 in this
# case. Basically, go back and print the last
# meaningful number
if not node.left and not node.right:
myList.append(node.val)
print(myList)
#trick to resolve the fact that I need two arguments
#root and root1. we do it separately. Then we assign
#contents to seq1 and put the result(s) in the initialized
#myList, then myList back to 0 and then dfs[root1]
#and boolean to returned the result
dfs(root)
seq1 = myList[:]
myList = []
dfs(root1) #need this for recursion to work
return seq1 == myList
root = node(1)
root.left = node(2)
#root.right = node(3)
root1 = node(1)
root1.left = None
root1.right = node(2)
myVar = Solution()
myVar.leafSimilar(root, root1)