-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_string_from_binary_tree.py
More file actions
74 lines (66 loc) · 1.65 KB
/
construct_string_from_binary_tree.py
File metadata and controls
74 lines (66 loc) · 1.65 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
This is correct. just for coding and testing.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def has_left(self):
return self.left != None
def has_right(self):
return self.right != None
def has_child(self):
return self.has_left() or self.has_right()
def solution(t):
if t is None:
print('()',end='')
return
print(t.val,end='')
if t.has_left():
print('(', end='')
solution(t.left)
print(')', end='')
if t.has_right():
if not t.has_left():
print('()', end='')
print('(', end='')
solution(t.right)
print(')', end='')
if __name__ == '__main__':
t = Node(1)
t.left = Node(2)
t.left.left = Node(4)
t.right = Node(3)
s = Node(1)
s.left = Node(2)
s.right = Node(3)
s.left.right = Node(4)
'''
#-------------Answer here---------------------
class Solution:
def tree2str(self, t):
"""
:type t: TreeNode
:rtype: str
"""
res = []
def f(t):
if t == None:
return
res.append(str(t.val))
if t.left != None:
res.append('(')
f(t.left)
res.append(')')
if t.right != None:
if t.left == None:
res.append('()')
res.append('(')
f(t.right)
res.append(')')
return res
result_str = f(t)
print(result_str)
if result_str == None:
return ''
return ''.join(result_str)