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.

题目要求用括号的形式表示一颗二叉树,如果右边的节点为空括号()可以省略,如果左节点为空,括号()不能省略。

class Solution {
public String tree2str(TreeNode t) {
if (t == null) return "";
String result = "" + t.val;//根节点
String left = tree2str(t.left); //左边
String right = tree2str(t.right); //右边
if(left.equals("") && right.equals("")) return result; //叶节点
if (left.equals("") ) return result + "()" + "(" + right + ")";
if (right.equals("") ) return result + "(" + left + ")";
return result + "(" + left + ")" + "(" + right + ")";
}
}
 
 
 
 

606. Construct String from Binary Tree的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. MyRapid WinForm 快速开发框架

    MyRapid 框架介绍开发历程:作者是数据库相关软件开发从业人员,懒惰的,能交给电脑做的事情懒得自己做开发目的:处理底层数据传输,减少工作量,提高开发效率框架特点:数据库相关开发.易学易用.快速上手 ...

  2. PHP+Redis 实例【一】点赞 + 热度 下篇

    这篇主要讲如何将数据保存回Mysql,但是里面还会涉及到如何将错误信息以及提示信息保存到文件里,方便以后的运维,再有就是如何使用PHP写进程BAT. Redis数据刷回数据库前的知识准备 首先针对上篇 ...

  3. win7下使用apache ab 比较测试node与 tomcat

    最近在研究node,都说node单线程.事件环机制,高并发效率高,亲测一下,一探究竟 apache ab 安装 进入:http://httpd.apache.org/download.cgi#apac ...

  4. jQuery与别的js框架冲突

    jQuery.noConflict()运行这个函数将变量$的控制权让渡给第一个实现它的那个库. 这有助于确保jQuery不会与其他库的$对象发生冲突. <script type="te ...

  5. jdk1.8hashmap源码解析

    /* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...

  6. uva 471 - Magic Numbers

    题意:给出一个数n,问有多少组数满足是s1/ s2 =n,要求组成s1和s2的数字没有重复的. 分析:枚举,然后二进制判断各位数字是否相同. #include<iostream> #inc ...

  7. 【JSOI2008】最大数

    https://www.luogu.org/problem/show?pid=1198 之前刚学完Splay想找题练手的时候做的,写完Splay交上去了才发现这应该是线段树裸题23333 Splay解 ...

  8. SpringBoot多数据源配置

    准备环境: jdk1.8 eclipse tomcat8.0 第一步:在配置文件添加如下信息: spring.datasource.primary.url=jdbc:mysql://localhost ...

  9. 浏览器抓包(post)

    谁能想到我写的第一个wp竟然是个web题??? 以Geek2017_Buy me a Tesla 为例 来看看题目 emmmm有理想还是很好的,火狐打开(事实证明FF对web题是最友好的) 能点的地方 ...

  10. jquery on()动态绑定元素的的点击事件无反应的问题记录

    1.jquery使用版本:v2.0 2.重现代码: html <table class="table"> <thead> <tr> <th ...