Leetcode 606. 根据二叉树创建字符串
题目链接
https://leetcode.com/problems/construct-string-from-binary-tree/description/
题目描述
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
示例 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)"
解释: 和第一个示例相似,
除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。
题解
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public String tree2str(TreeNode t) {
StringBuilder sb = new StringBuilder();
backTrack(t, sb);
return sb.toString();
}
public void backTrack(TreeNode t, StringBuilder sb) {
if (t == null) { return ; }
sb.append(t.val);
if (t.left != null || t.right != null) {
sb.append("(");
backTrack(t.left, sb);
sb.append(")");
}
if (t.right != null) {
sb.append("(");
backTrack(t.right, sb);
sb.append(")");
}
}
}
Leetcode 606. 根据二叉树创建字符串的更多相关文章
- Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
606. 根据二叉树创建字符串 你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原 ...
- 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 ...
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
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 ...
- Q606 根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- 【python】Leetcode每日一题-扰乱字符串
[python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...
- java中创建字符串的两种方式(“”与new String())及区别
结论:通过""创建的字符串实际上在java堆中只有一个,而通过new string创建出来的字符串在java堆中占有不同的内存. 第一个True表明这两个在内存中拥有相同的地址,那 ...
随机推荐
- python基础:冒泡和选择排序算法实现
冒泡排序和选择排序 首先引用一下百度百科对于冒泡算法的定义: 冒泡排序算法的原理如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素做同样的工作,从开始第一对到结尾 ...
- sass文件转css时注释虽然支持中文,但是出现乱码的解决方法
sass文件转css时注释虽然支持中文,但是出现乱码的解决方法 Scss 注释中文报错问题(windows系统, 已解决)找到ruby的安装目录,里面也有sass模块,类似这样样的路径:F:\Prog ...
- canvas的arcTo API
- iOS开发之Objective-c的AES256加密和解密算法的实现
原文:http://www.lidaren.com/archives/1470 高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法. 以下实现 ...
- arm寄存器解析
寒假闲来无事准备将自己的走过的arm之路总结一下,今天就先从arm的寄存器说起吧,欢迎各位拍砖. 要介绍arm寄存器之前我们要先了解一下arm处理器的工作模式: Arm处理器有七种工作模式,为的是形成 ...
- selenium Element not found in the cache - perhaps the page has changed since it was looked up接解决
selenium Element not found in the cache - perhaps the page has changed since it was looked up.这个问题爆出 ...
- ansible安装php
环境:Centos 7.x 独立php-fpm.conf配置文件 [root@master playbook]# tree php php ├── php-fpm.conf └── php.yml p ...
- 给网站添加图标: Font Awesome
先贴上各种图标的网址:https://fontawesome.com/?from=io 1.打开网址,我们可以看到: 像我这种英语不好的,直接用谷歌浏览器进行翻译中文好了,自己还是有点B数的····· ...
- Vsftpd服务传输文件(转)
本章节先通过介绍文件传输协议来帮助读者理解FTP协议的用处,安装vsftpd服务程序并逐条分析服务文件的配置参数. 完整演示vsftpd服务匿名访问模式.本地用户模式及虚拟用户模式的配置方法,介绍PA ...
- CORS跨域限制以及预请求验证
之前我们可以通过“Access-Control-Allow-Origin”,实现跨域请求,那是不是所有跨域请求都可以通过设置Access-Control-Allow-Origin实现跨域请求呢?显然不 ...