[LeetCode&Python] Problem 606. Construct String from Binary Tree
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.
# 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 is None: return ""
res=str(t.val)
left=self.tree2str(t.left)
right=self.tree2str(t.right)
# if a root doesn't have left children but has right children,
# we still need to us "()" to represent it.
if t.right or t.left:
res=res+"("+left+")"
if t.right:
res=res+"("+right+")" return res
[LeetCode&Python] Problem 606. Construct String from Binary Tree的更多相关文章
- 【Leetcode_easy】606. Construct String from Binary Tree
problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...
- LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)
题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 606. Construct String from Binary Tree 从二叉树中构建字符串
[抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...
随机推荐
- 使用virustotal VT 查询情报——感觉远远没有微步、思科好用,10万条数据查出来5万条都有postives >0的记录,尼玛!!!
1399 git clone https://github.com/VirusTotal/c-vtapi.git 1400 cd c-vtapi/ 1402 sudo apt-get install ...
- fiddler 抓包配置
1.我们安装完成后点击运行程序,就可以看到如下图,这是进入Fildder的第一个界面. 2.安装好后打开fiddler→选择 Tools >Fildder Options > Https ...
- 字符串和数组----vector
vector能容纳绝大多数类型的对象作为其元素,但是因为引用不是对象,所以不存在包含引用的vector. 使用vector需要包含头文件vector. 1.初始化vector对象的方法 1)vecto ...
- word个人信息的一种处理方式
下面是一种解决office文件更改作者的方法,步骤如下: 第一步:进入Word,Excel,或PowerPoint, 随便打开一个文件: 第二步:点击左上角的文件,进入文件功能界面: 第三步:点击选项 ...
- sql语句最后一行显示统计。
SELECT id, username, id_Num FROM users UNION ALL SELECT '合计', count(*), null FROM users ORDER BY id_ ...
- MATLAB图片折腾3
把视频抽帧,转化成图片 我的代码如下,成功实现clc;clear;videofilename='k:\GraduationWork\Resource\video.wmv'; %where you pu ...
- 《Python》并发编程
手工操作 —— 穿孔卡片 1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作方式.此时还没有操作系统的概念. 程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入输 ...
- APC注入(Ring3)
首先简单介绍一下APC队列和Alertable. 看看MSDN上的一段介绍(https://msdn.microsoft.com/en-us/library/ms810047.aspx): The s ...
- Office 365 企业应用以及服务概览 分享记录
博客地址:http://blog.csdn.net/FoxDave 分享时间: 2017年9月14日 分享地点: 部门内部 参与人数: 16人 分享内容: 讲解微软MVP项目计划的相关内容:讲解O ...
- poj1062(分区间迪杰斯特拉,内含测试数据,一直wa的同学可以进来看看)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54946 Accepted: 16518 Descripti ...