Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
示例 1:
输入:
二叉树: [1,2,3,4]
1
/ \
2 3
/
4
输出: "1(2(4))(3)"
解释: 原本将是“1(2(4)())(3())”, 在你省略所有不必要的空括号对之后, 它将是“1(2(4))(3)”。
示例 2:
输入:
二叉树: [1,2,3,null,4]
1
/ \
2 3
\ 4
输出: "1(2()(4))(3)" 解释: 和第一个示例相似, 除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
string res = "";
string tree2str(TreeNode* t) {
if (t == NULL)
return res;
GetAns(t);
return res;
}
void GetAns(TreeNode* root)
{
if(root != NULL)
res += to_string(root->val);
if (root != NULL && (root->right != NULL || root->left != NULL))
{
res += '(';
GetAns(root->left);
res += ')';
}
if (root != NULL && root->right != NULL)
{
res += '(';
GetAns(root->right);
res += ')';
}
}
};
Leetcode606.Construct String from Binary Tree根据二叉树创建字符串的更多相关文章
- [LeetCode] 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根据二叉树创建字符串 (C++)
题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- 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 ...
- [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 (建立一个二叉树的string)
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 606. 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 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
随机推荐
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- PAT甲级——【牛客练习A1004】
题目描述 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For ex ...
- Chsh- Linux必学的60个命令
1.作用 chsh命令的作用是更改使用者shell设定,它的使用权限是所有使用者. 2.格式 chsh [ -s ] [ -list] [ --help ] [ -v ] [ username ] 3 ...
- Odoo Documentation : Recordsets
Other recordset operations Recordsets are iterable(可迭代的) so the usual Python tools are available for ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--08--06CMDB测试Linux系统采集硬件数据的命令03
https://www.virtualbox.org/wiki/Downloads https://mirrors.aliyun.com/centos/7/isos/x86_64/ http://ww ...
- Django项目:CMDB(服务器硬件资产自动采集系统)--01--01CMDB获取服务器基本信息
AutoClient #settings.py # ————————01CMDB获取服务器基本信息———————— import os BASEDIR = os.path.dirname(os.pat ...
- webpack打包css文件
1. 安装css-loader 与 style-loader npm install style-loader css-loader --save-dev 2. 在webpack.config.js中 ...
- Filter - 全局编码 (装饰者模式)
Tomcat7以前需要处理get乱码,tomcat8以后不需要处理get乱码,因为tomcat内部会处理. 乱码原因: package cn.itcast.web.filter; import jav ...
- Http请求中的Content-Type
转载至:https://segmentfault.com/a/1190000013056786?utm_source=tag-newest 一 前言 ----现在搞前端的不学好http有关的知识已经不 ...
- 关于使用注解设置token免拦截 和 必要 的权限拦截
先说一下这样子做的原理:将某一个注解配置在方法头部,在spring实例化的时候会将注解以切面的形式注入给方法,在拦截的地方判断当前方法有没有注入指定的注解类. 1.先声明一个注解类(类中不需要做任何逻 ...