-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs_606_constructStringfromBinaryTree.py
More file actions
41 lines (31 loc) · 1.07 KB
/
dfs_606_constructStringfromBinaryTree.py
File metadata and controls
41 lines (31 loc) · 1.07 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
"""
https://leetcode.com/problems/construct-string-from-binary-tree/
leetcode 606
easy
input : root of a binary tree
output: construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Logic : dfs with recursion : preorder traversal
Time Complexity: o(n)
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def tree2str(self, root) -> str:
res = []
def dfs(root):
if not root :
return
res.append("(")
res.append(str(root.val))
if not root.left and root.right :
res.append("()")
dfs(root.left)
dfs(root.right)
res.append(")")
dfs(root)
return "".join(res)[1:-1]