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 ...
随机推荐
- VMware的下载安装
在学习使用LINNX系统之前,先在自己的电脑上安装一个虚拟机,流行的虚拟机软件有VMware(VMWare ACE).Virtual Box和Virtual PC,它们都能在Windows系统上虚拟出 ...
- vue 单纯的获取经纬度 百度与高德 H5
首先用百度的api举个例子 首先在index页面引入如下: <script type="text/javascript" src="http://api.map.b ...
- NSLayoutConstraint-代码实现自动布局的函数用法说明
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbownight.blog.51cto.com/1336585/13161 ...
- HDU--2602(0-1背包)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2602 分析:一个0-1背包问题.记得<背包九讲>的方法. dp[j]=max{dp[j],d ...
- js 使用script或template标签:分离js代码template中的HTML元素
参考:https://www.jianshu.com/p/332252abe016 方法一. script: <div id="app"> <com-first& ...
- Hibernate调用Oracle的存储过程
众所周知,当过多的使用存储过程,触发器等 数据库方言相关的应用时,应用程序的移植性会变差,特别是在Hibernate中使用这些,简直是讽刺,但是当今中国又有哪家公司做项目会关心应用程序的移植性呢? 现 ...
- request与session的区别
request对象和session对象的最大区别是生命周期与范围. request request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用 ...
- Leetcode946. Validate Stack Sequences验证栈序列
给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入:pus ...
- ssh 免密码登入
1.普通免密码登入 (1) 生成秘钥 [root@vick ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter f ...
- light oj 1068 数位dp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...