# 606. Construct String from Binary Tree 根据二叉树创建字符串

@TOC

## # 题目描述

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

Example 1:

``````Input: Binary tree: [1,2,3,4]
1
/   \
2     3
/
4

Output: "1(2(4))(3)"

Explanation: Originallay it needs to be "1(2(4)())(3()())",
but you need to omit all the unnecessary empty parenthesis pairs.
And it will be "1(2(4))(3)".
``````

Example 2:

``````Input: Binary tree: [1,2,3,null,4]
1
/   \
2     3
\
4

Output: "1(2()(4))(3)"

Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
``````

## # 解题方法

### # 方法一：先序遍历

1. 对于左子树，如果左孩子或者右孩子存在，那么左子树一定有括号；如果左右子树都不存在，那么为空字符串
2. 对于右子树，如果右子树存在，则是有括号的；如果有子树不存在，那么就是空字符串；
3. 最后的结果是根节点的值加上左右子树的字符串
``````# 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 ''
subleft = '({})'.format(self.tree2str(t.left)) if t.left or t.right else ''
subright = '({})'.format(self.tree2str(t.right)) if t.right else ''
return '{}{}{}'.format(t.val, subleft, subright)
``````

## # 日期

2018 年 1 月 21 日 2018 年 11 月 11 日 —— 剁手节快乐