-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconstruct.py
More file actions
33 lines (30 loc) · 945 Bytes
/
construct.py
File metadata and controls
33 lines (30 loc) · 945 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
31
32
33
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def tree2str(self, t):
"""
:type t: TreeNode
:rtype: str
"""
def pre_order(node):
print node
if node and node.val:
result.append(str(node.val))
if node.left:
result.append("(")
pre_order(node.left)
result.append(")")
if node.right:
result.append("(")
pre_order(node.right)
result.append(")")
else:
result.append("()")
# Order matters, watch the order of execution while writing a problem
result = []
pre_order(t)
return "".join(result)