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的更多相关文章

  1. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  2. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  3. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  4. Python 解LeetCode:606 Construct String from Binary Tree

    题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...

  5. 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 ...

  6. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  7. 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 ...

  8. 606. Construct String from Binary Tree

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

  9. 606. Construct String from Binary Tree 从二叉树中构建字符串

    [抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...

随机推荐

  1. Oracle12c中配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB)

    Oracle12c中的多宿主选项允许一个容器数据库(CDB)容纳多个独立的可插拔数据库(PDB).本文将展示如何配置实例参数和修改容器数据库(CDB)及可插拔数据库(PDB).1. 配置CDB中的实例 ...

  2. Linux变量及运算

    变量赋值:var=var_value 变量引用:$var 算术运算:var=`expr $var1 + $var2` 字符串连接:var=str$var1 数值比较:-eq/-ne/-gt/-lt/- ...

  3. anglar cli的 rxjs_1.of is not a function

    按照官网敲例子遇到 rxjs_1.of is not a function import { Observable,of } from 'rxjs'; 改为 import { Observable} ...

  4. Guidelines for Writing a Good NIPS Paper

    By the NIPS 2006 Program Committee With input from Andrew Ng, Peter Dayan, Daphne Koller, Sebastian ...

  5. Win10系列:VC++调用自定义组件1

    通过20.9.1小节中的代码和步骤编写了一个名为"FilePickerComponent"的WinRT组件,接下来将在上一小节所新建的项目基础上,继续介绍如何在不同的语言所编写的应 ...

  6. 这本小书的目的是引导你进入 React 和 Webpack 的世界。他们两个都是非常有用的技术,如果同时使用他们,前端开发会更加有趣。

    https://fakefish.github.io/react-webpack-cookbook/index.html

  7. 你应该这样理解JVM内存管理

    在进行Java程序设计时,一般不涉及内存的分配和内存回收的相关代码,此处引用一句话: Java和C++之间存在一堵由内存动态分配和垃圾收集技术所围成的高墙,墙外的人想进去,墙里面的人想出来 ,个人从这 ...

  8. Linux运维工程师真实的工作状态到底是怎么样的?

    现在的运维工程师在大家眼中是个什么样子呢? 是不是还是把服务器搬来搬去,每天不是在拿着Linux光盘开始装系统,就是在等待系统安装完成.你如果还是这么想,那就大错特错了.现在又有做一个新的物种诞生,那 ...

  9. 给msde加装企业管理器

    -=给msde加装企业管理器=- 首先,反对所谓的绿色版,运行那是 相~~~当 不稳定,自动关闭,要你有什么用?还广告飞扬!为了调试,花了我整整一天的时间.给大家节省的时间,也为了让大家少走点弯路. ...

  10. Java反射(一眼就看会)

    参考:(1)http://blog.csdn.net/liujiahan629629/article/details/18013523(2)https://www.zhihu.com/question ...