[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 ...
随机推荐
- 使用JQuery 合并两个 json 对象
一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 var object = $.extend({}, object1, object2); 二,将2的值合并到 ...
- Vue 导入excel功能
html: <input type="file" @change="importf(this)" accept=".csv, applicati ...
- Apache升级PHP教程(以5.3.3升级到5.6.30为例)
最简单的LAMP环境搭建当然是通过yum来安装,但由于镜像仓库中的软件版本更新较慢,经常会遇到版本过旧的问题,尤其是安装一些新版本的CMS时的PHP. 这时我们需要手动编译PHP,Linux编译安装经 ...
- javascript中的require、import和export模块文件
CommonJS 方式 文件输出如math.js: math.add = function(a,b){ return a+b; }exports.math = math; 文件引入: math = r ...
- anglar cli的 rxjs_1.of is not a function
按照官网敲例子遇到 rxjs_1.of is not a function import { Observable,of } from 'rxjs'; 改为 import { Observable} ...
- 运行网站项目时,有时出现Bad Request,该怎么解决?
有时运行网站项目时,出现Bad Request问题
- laravel创建新的提交数据
public function store() { $this->validate(request(),[ 'title'=>'required|string|max:100|min:10 ...
- 【转】Java中static关键字用法总结
1. 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: · 它们仅能调用其他的static 方法. · 它 ...
- vs2013 跳过IE10
安装 VS2013要安装IE10,却安装不了..以下为跳过IE10,直接安装VS2013方法 复制以下代码,保存为bat文件 @ECHO OFF :IE10HACK REG ADD "HKL ...
- linux Bash 常用
linux 帮助文档 man + [命令] eg: man ls[命令] + --help eg:ls --helphelp +[命令] eg:help ceinfo + [命令] eg:info l ...