[LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
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 根据二叉树创建字符串的更多相关文章
- 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 ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- LeetCode Construct String from Binary Tree
原题链接在这里:https://leetcode.com/problems/construct-string-from-binary-tree/#/description 题目: You need t ...
- 606. Construct String from Binary Tree 从二叉树中构建字符串
[抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【Leetcode_easy】606. Construct String from Binary Tree
problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...
- 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 ...
- [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 ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
随机推荐
- oracle--dba和表的备份与恢复
数据库管理员 每个oracle数据库应该至少有一名数据库管理员(dba),对于一个小的数据库,一个dba就够了,但是对于一个大的数据库可能需要多个dba分别担负不同的管理职责,那么一个数据库管理员的主 ...
- 网络推广 免费推广产品网站 B2B网站如何推广
云集网(yunjinet.com)免费发布各类服务和产品信息,在平台上推广你的产品.帮助商家推广优质的产品和服务.如何提高信息的点击量为了提高分类信息网的信息质量,对重复度高.相似度高的信息进行了过滤 ...
- C语言第一次博客作业 陈张鑫
一,PTA实验作业 题目1.温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代 ...
- C语言的第 次作业总结
PTA实验作业 第一题: 使用函数输出水仙花数 1.设计思路: 2.碰到的问题及解决方法: 实验中碰到的主要问题是:虽然知道如何求每一位的数但不知道如何输出m到n之间的水仙花数,我上面截图中的和瓮恺视 ...
- 按指定id顺序查询
SELECT a.p,* FROM tb1 t,( as p union as p union as p union as p union as p ) a where t.id=a.id order ...
- bug终结者 团队作业第六、七周
bug终结者 团队作业第六.七周 作业要求:团队作业第六.七周 博客编辑:20162322 朱娅霖 一.修改<需求规格说明书> <需求规格说明书>2.0版(即初稿) <需 ...
- Alpha冲刺Day9
Alpha冲刺Day9 一:站立式会议 今日安排: 经过为期5天的冲刺,基本完成企业人员模块的开发.因第三方机构与企业存在委托的关系.第三方人员对于风险的自查.风险列表的展示以及自查风险的统计展示(包 ...
- Java的HelloWorld程序
Java的HelloWorld程序 1.新建文本文档,编写HelloWorld程序,最后保存时记得保存成.java格式 2.在D盘新建一个HelloJava文件夹用于保存java程序 3.使用WIN+ ...
- python的dir、help、str用法
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表.当没有为其提供参数时, 它返回当前模块中定义的名字的列表.dir() 函数使用举例: 1 2 3 4 5 6 >>& ...
- Huginn实现自动通过slack推送豆瓣高分电影
博客搬迁至https://blog.wangjiegulu.com RSS订阅:https://blog.wangjiegulu.com/feed.xml 原文链接:https://blog.wang ...