- 606. Construct String from Binar
- 606. Construct String from Binar
- 606. Construct String from Binar
- 606. Construct String from Binar
- 606. Construct String from Binar
- 606. Construct String from Binar
- Leetcode PHP题解--D92 606. Constru
- 【1错-1】Construct String from Bina
- [LeetCode]606. Construct String
- 536. Construct Binary Tree from
606. Construct String from Binary Tree
![](https://img.haomeiwen.com/i17368230/c5fd8ca37cdec7f0.png)
![](https://img.haomeiwen.com/i17368230/de5d0f18a7574b32.png)
好久不刷题,简单题都不会了=_=,看来刷题不能停。
# 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 t == None:
return ''
if t.left == None and t.right == None:
return str(t.val)
elif t.left and t.right:
return str(t.val) + '(' + self.tree2str(t.left) + ')' + '(' + self.tree2str(t.right) + ')'
elif t.left:
return str(t.val) + '(' + self.tree2str(t.left) + ')'
else:
return str(t.val) + '()' + '(' + self.tree2str(t.right) + ')'
网友评论