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. 对于树,第一步一定要想到用递归。
 public class Solution {
public String tree2str(TreeNode root) {
if (root == null) return "";
String rootValue = String.valueOf(root.val);
String leftValue = tree2str(root.left);
String rightValue = tree2str(root.right); if (leftValue.equals("") && rightValue.equals("")) {
return rootValue;
} else if (leftValue.equals("")) {
return String.format("%s()(%s)", rootValue, rightValue);
} else if (rightValue.equals("")) {
return String.format("%s(%s)", rootValue, leftValue);
} else {
return String.format("%s(%s)(%s)", rootValue, leftValue, rightValue);
}
}
}

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. 606. Construct String from Binary Tree

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

  5. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

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

  6. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

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

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

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

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

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

  10. [Algorithm] Construct String from Binary Tree

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

随机推荐

  1. EntityManager的基本方法

    1.Persistence 主要用来获取EntityManagerFactory的实例; 通过静态方法:createEntityManagerFactory 来实现: 该方法有两个重载版本:     ...

  2. Redis配置文件中bind参数

    前言 我们都知道,redis 的配置文件中,默认绑定接口是 127.0.0.1,也就是本地回环接口,所以是无法从外网连接 redis 服务的.如果想要让外网也能连接使用服务器上的 redis 服务,可 ...

  3. linux下源码安装mariadb

    1.mariadb源码包下载地址:https://downloads.mariadb.org/ 2.安装mariadb是依赖包,创建mysql用户和目录: 命令  yum -y install rea ...

  4. P1427 小鱼的数字游戏

    输入格式: 一行内输入一串整数,以0结束,以空格间隔. 输出格式: 一行内倒着输出这一串整数,以空格间隔. 直接上代码: #include<iostream> using namespac ...

  5. 流程控制(判断if switch)

    判断语句 判断条件比特别多大 时候用switch 其他时候if语句比较方便   1.if……else a) if(判断条件) {执行语句:}   b) else if (判断语句){执行语句:}   ...

  6. C++入门经典-例7.8-const对象,标准尺寸

    1:当建立一个对象之后,如果不希望它的任何数据发生改变,可以将其直接声明为const对象,例如: const 类名 对象名 const对象必须初始化.我们可以调用它的数据和函数,但是不可以对他们进行修 ...

  7. linu逻辑分区动态调整大小

    注意: 这个动态调整的方法是有丢数据风险的,要确保调整的源分区没有使用或者使用率很低.源分区中如果有重要的文件最好先备份 在centos 6.5上操作过 lvdisplay 查看已有的分区的大小 lv ...

  8. 解释HTTP中Get和Post。它们有什么区别,哪个使用时更加安全?

    Get和Post都是浏览器向网页服务器提交数据的方法. Get把要提交的数据编码在url中,比如/workinfo.jsp/mianshiti?key1=value1&key2=value2中 ...

  9. Mybatis框架学习1:入门

    一框架介绍 1.Mybatis介绍 ​ MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google c ...

  10. 使用 Itext 生成PDF字节数组(文件流不落地)

    package com.ulic.gis.customerCenter.controller; import java.io.ByteArrayOutputStream; import java.io ...