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.

这道题给我们了一个二叉树,让我们创建对应的字符串,之前有一道正好反过来的题Construct Binary Tree from String。对于二叉树的处理,递归肯定是王道啊。想想如何来实现递归函数,我们观察到题目中的例子,发现如果左子结点为空,右子结点不为空时,需要在父结点后加上个空括号,而右子结点如果不存在,或者左右子结点都不存在就不需要这么做。那我们在递归函数中,如果当前结点不存在,直接返回,然后要在当前结点值前面加上左括号,然后判断,如果左子结点不存在,而右子结点存在的话,要在结果res后加上个空括号,然后分别对左右子结点调用递归函数,调用完之后要加上右括号,形成封闭的括号。由于最外面一层的括号不需要,所以我们再返回最终结果之前要去掉首尾的括号,参见代码如下:

解法一:
class Solution {
public:
string tree2str(TreeNode* t) {
if (!t) return "";
string res = "";
helper(t, res);
return string(res.begin() + , res.end() - );
}
void helper(TreeNode* t, string& res) {
if (!t) return;
res += "(" + to_string(t->val);
if (!t->left && t->right) res += "()";
helper(t->left, res);
helper(t->right, res);
res += ")";
}
};

下面来看一种不用额外函数的递归写法,这种做法是一开始调用递归函数求出左右子结点的返回字符串,如果左右结果串均为空,则直接返回当前结点值;如果左子结果串为空,那么返回当前结果res,加上一个空括号,再加上放在括号中的右子结果串;如果右子结果串为空,那么发返回当前结果res,加上放在括号中的左子结果串;如果左右子结果串都存在,那么返回当前结果,加上分别放在括号中的左右子结果串,参见代码如下:

解法二:

class Solution {
public:
string tree2str(TreeNode* t) {
if (!t) return "";
string res = to_string(t->val);
string left = tree2str(t->left), right = tree2str(t->right);
if (left == "" && right == "") return res;
if (left == "") return res + "()" + "(" + right + ")";
if (right == "") return res + "(" + left + ")";
return res + "(" + left + ")" + "(" + right + ")";
}
};

下面这种解法更加简洁,由热心网友edyyy提供,思路和上面解法相同,参见代码如下:

解法三:

class Solution {
public:
string tree2str(TreeNode* t) {
if (!t) return "";
string res = to_string(t->val);
if (!t->left && !t->right) return res;
res += "(" + tree2str(t->left) + ")";
if (t->right) res += "(" + tree2str(t->right) + ")";
return res;
}
};

类似题目:

Construct Binary Tree from String

参考资料:

https://discuss.leetcode.com/topic/91308/java-solution-tree-traversal

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Construct String from Binary Tree 根据二叉树创建字符串的更多相关文章

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

  2. Leetcode606.Construct String from Binary Tree根据二叉树创建字符串

    你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...

  3. LeetCode Construct String from Binary Tree

    原题链接在这里:https://leetcode.com/problems/construct-string-from-binary-tree/#/description 题目: You need t ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 在Python中使用Redis

    在Python中要使用Redis数据库,首先要安装redis 之前的博客中有写到在命令行模式下操作Redis数据库. 要在项目中使用的话可以这么做: 通过初始化 redis.Redis,得到返回的对象 ...

  2. 记录某公司(简称SMKJ) 的一次面试

    昨天去了一家公司面试 Java 开发岗位,这篇文章主要是做一个面试的记录以及总结. 这家公司的规模大概100-200人,环境还可以,在一栋大厦租了两层办公室(31层和32层).一同搭电梯上去的还有一位 ...

  3. JAVA读取Excel中内容(HSSF和Workbook两种方法)

    内容添加,以前是用的HSSF,前几天帮同学写一个统计表用了Workbook,现在码一下. ---新内容(Workbook)--- 同学要统计一个xls表格,让表1里面的某一列内容对表2里面的每列进行匹 ...

  4. 关于如何学习C语言

    2016级计算机专业的C语言分为两个学期,第一学期是C语言(基础),第二学期是C语言(高级),在第一学期主要学习的内容是基本的数据类型,分支结构和循环结构,一维和二维数组,字符数组,函数.通过这学期独 ...

  5. C语言博客作业--一二维数组。

    一.PTA实验作业 题目1:7-1 将数组中的数逆序存放 1. 本题PTA提交列表 2. 设计思路 定义三个整型变量n用来存放整数个数i,j是循环数 scanf("%d",& ...

  6. C语言实现Linux命令——od

    C语言实现Linux命令--od 实现要求: 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写M ...

  7. Scapy实现SYN泛洪攻击

    一.实验说明 1.实验介绍 本次实验将使用python3版本的Scapy--Scapy3k来实现一个简单的DDos,本次实验分为两节,本节将学习如何使用Scapy3k来实现SYN泛洪攻击. 2.知识点 ...

  8. 51Nod P1100 斜率最大

    传送门: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 由于2 <= N <= 10000, 所以 ...

  9. NOIP2016 天天爱跑步 正解

    暴力移步 http://www.cnblogs.com/TheRoadToTheGold/p/6673430.html 首先解决本题应用的知识点: dfs序——将求子树的信息(树形)转化为求一段连续区 ...

  10. Java代码风格和在idea中的一些设置

    源文件基本设置 1. 文件名 驼峰标识,.java结尾 2. 编码 统一为UTF-8 Transport...可以解决property文件不能正常显示为中文的问题 3. 特殊字符 尽量使用转义字符(\ ...