Leetcode 606.根据二叉树创建字符串

606. 根据二叉树创建字符串

题目

你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。

空节点则用一对空括号 “()” 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
示例 1:

输入: 二叉树: [1,2,3,4]
1
/ \
2 3
/
4

输出: "1(2(4))(3)"

解释: 原本将是“1(2(4)())(3())”,
在你省略所有不必要的空括号对之后,
它将是“1(2(4))(3)”。
示例 2:

输入: 二叉树: [1,2,3,null,4]
1
/ \
2 3
\
4

输出: "1(2()(4))(3)"

解释: 和第一个示例相似,
除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

方法

方法1:递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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
"""
if not t: return ''
if not t.left and not t.right: return str(t.val)

if not t.left: return str(t.val) + '()' + '(' + self.tree2str(t.right) + ')'
if not t.right: return str(t.val) + '(' + self.tree2str(t.left) + ')'
return str(t.val) + '(' + self.tree2str(t.left) + ')' + '(' + self.tree2str(t.right) + ')'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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
"""
if not t:
return ""
res = str(t.val)
if t.left or t.right:
res += "(" + self.tree2str(t.left) + ")"
if t.right:
res += "(" + self.tree2str(t.right) + ")"
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
"""
if not t:
return ''

ans=str(t.val)
if t.left:
ans+='('+self.tree2str(t.left)+')'
if not t.left and t.right:
ans+='()'
if t.right:
ans+='('+self.tree2str(t.right)+')'
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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
"""
if not t:
return ''
if not t.left and not t.right:
return str(t.val)+''
if not t.right:
return str(t.val)+'('+self.tree2str(t.left)+')'
else:
return str(t.val)+"("+self.tree2str(t.left)+")("+self.tree2str(t.right)+")"
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
# 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
"""
if not t:
return ''
s=str(t.val)
left=self.tree2str(t.left)
right=self.tree2str(t.right)

if left=='' and right=='':
return s
elif left=='':
s+='()'+'('+right+')'
elif right=='':
s+='('+left+')'
else:
s+='('+left+')'+'('+right+')'
return s

方法2:迭代

未完成

参考资料