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. [winafl]这几天的折腾

    1.自己写的exe 2.自己写的dll,然后写了接口程序去load...但是这个速度真是迷醉 先把基本的几种类型跑起来,再解决速度和样本的问题...

  2. 2018 焦作网络赛 K Transport Ship ( 二进制优化 01 背包 )

    题目链接 题意 : 给出若干个物品的数量和单个的重量.问你能不能刚好组成总重 S 分析 : 由于物品过多.想到二进制优化 其实这篇博客就是存个二进制优化的写法 关于二进制优化的详情.百度一下有更多资料 ...

  3. Gulp error in WebStorm: Failed to list gulp tasks

    I have the same problem with webstorm after install a updated version of node. The solution for me i ...

  4. 超好用的富文本编辑器Summernote的使用

    官网地址 中文文档 源码下载地址 Summernote依赖于jquery和bootstrap3/4   所以用时记得引入这俩依赖 奉上引入方法(官网说的很清楚,api也很详细): <!-- in ...

  5. AtCoder AGC009E Eternal Average (DP)

    题目链接 https://atcoder.jp/contests/agc009/tasks/agc009_e 题解 又被劝退了... 第一步转化非常显然: 就等价于一开始有一个数\(1\), 有\(\ ...

  6. 认识并安装RabbitMQ(以Windows系统为例)

    一.初识RabbitMQ 百度百科有这么一句话: MQ是消费-生产者模型的一个典型的代表,一端往消息队列中不断写入消息, 而另一端则可以读取或者订阅队列中的消息. MQ和JMS类似,但不同的是JMS是 ...

  7. C++入门经典-例6.22-字符串与数组,string类型的数组

    1:数组中存储的数据也可以是string类型的.代码如下: // 6.22.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include ...

  8. Springdata-Jpa学习笔记

    Respository接口 Respository是Springdata JPA中的顶层接口,提供了两种查询方法: 1)基于方法名称命名规则 2)基于@Qeury注解查询 1. 方法名称命名规则查询 ...

  9. 3 Java 冒泡排序法

    冒泡排序( Bubble Sort)是一种简单的排序算法.它重复访问要数列, 一次比较两个元素,如果他们的顺序错误就把交换过来.访问数列工作是 一次比较两个元素,如果他们的顺序错误就把交换过来.访问数 ...

  10. JDBC——JDBC基础

    1.JDBC与数据库的交互过程概括性来说,JDBC与数据库交互有以下这些步骤:1.建立一个到数据库的连接.2.在数据库中对表执行检索.创建,或修改的SQL查询.3.关闭到数据库的连接.JDBC的类和接 ...