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 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.
分析:
给定一颗二叉树,按要求创建字符串。
我们可以发现,一个节点如果有左右节点的话,是要用()括起来左右节点,如果left存在,righ不存在,可以省略掉右边的括号,如果left不存在,right存在,左括号是不能省略的。
程序:
class Solution {
public:
string tree2str(TreeNode* t) {
string s = "";
if(t == nullptr) return s;
if(!t->left && !t->right)
return s + to_string(t->val);
if(t->right == nullptr)
return s + to_string(t->val) + "(" + tree2str(t->left) + ")";
return s + to_string(t->val) + "(" + tree2str(t->left) + ")("+ tree2str(t->right) + ")";
}
};
LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)的更多相关文章
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- 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 ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
随机推荐
- php输出日志
error_log('你要输出的信息', 3, 'E:\work\jiajiayue\Application\Api\Controller\1.txt');die; php error_log记录日志 ...
- Docker Java应用日志时间和容器时间不一致
1.在docker容器和系统时间不一致是因为docker容器的原生时区为0时区,而国内系统为东八区. 2.还有容器中运行的java应用打出的日志时间和通过date -R方式获取的容器标准时间有八个小时 ...
- Android 混淆打包
有些时候我们希望我们自己的apk包不能被别人反编译而获取自己的源代码.这就需要我们通过Android提供的混淆打包技术来完成. 一.没有引用外部包的情况: 这种情况下代码混淆的方式相对简单: 1)只需 ...
- Android 一个相对完整的自动升级功能实现代码
由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...
- python3 面向对象编程--类的封装和继承
#python3import refrom urllib import requestimport os class PosterSpider(object): def __init__(se ...
- kubernetes 的wen pod 无法连接 mysql 的pod
1.分析 查看源代码 既然无法建立连接,那先看下是如何建立连接的.登录到myweb的docker容器里面,查看index.jsp文件,主要内容如下: Class.forName("com.m ...
- 解决Win10家庭版没有‘本地用户和组’问题
今天偶然发现我的win10系统是家庭版,并且没有本地用户和组. 处理方法:将系统升至为win10专业版,然后下载microKMS_v17.02.14做的激活.参考网站 1.打开运行窗口,输入 gped ...
- Scala--特质
一.为什么没有多重继承 c++允许多重继承 Java不允许多重继承,类只能继承一个超类,可以实现任意数量的接口. 如何继承这两个抽象基类? Scala提供“特质”而非接口:特质可以同时抽象方法和具体方 ...
- 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI
一.hbase数据模型 完整的官方文档的翻译,参考:https://www.cnblogs.com/simple-focus/p/6198329.html 1.rowkey 与nosql数据库们一样, ...
- XAMPP、PHPstorm和PHPcharm和Windows环境下Python搭建+暴力破解
XAMPP的安装和使用 一.什么是XAMPP? XAMPP是最流行的PHP开发环境. XAMPP是完全免费且易于安装的Apache发行版,其中包含Apache.MariaDB.PHP和Perl. 类似 ...