606. 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.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
数字毕竟不熟字符串,数字+双引号""才是,这是计算机思维
[奇葩corner case]:
[思维问题]:
DFS中是可以分为step1 step2的,因为下一次DFS之前肯定会走step2
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 字符串的题判断空的条件是 == "",头一次见 注意下
- 先dfs, 后操作,所以没理解到位的事 都是对dfs的结果left right操作
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
- 检查函数返回值,不是void型就要返回正常值(所以要提前注释)
[总结]:
字符串变量反而不需要加引号 因为已经规范化了,字符串要加
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
stringbuilder不好用,直接用+建字符串就行
[关键模板化代码]:
新建字符串模板
if (left == "" && right == "") return ans;
//left is null
if (left == "") return ans + "()" + "(" + right + ")";
//right is null
if (right == "") return ans + "(" + left + ")";
//return
return ans + "(" + left + ")" + "(" + right + ")";
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
536. Construct Binary Tree from String 反过来:高级版string文字游戏,唉
[代码风格] :
/**
* 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) {
//corner case
if (t == null) {
return "";
}
//ini
String ans = t.val + "";
//dfs
String left = tree2str(t.left);
String right = tree2str(t.right);
//build string
//exit
if (left == "" && right == "") return ans;
//left is null
if (left == "") return ans + "()" + "(" + right + ")";
//right is null
if (right == "") return ans + "(" + left + ")";
//return
return ans + "(" + left + ")" + "(" + right + ")";
}
}
606. 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 ...
- 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 ...
- 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] 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 ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- [LeetCode&Python] Problem 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
随机推荐
- php mysql 字符集(三) (转)
http://bbs.csdn.net/topics/390097514 gbk页面插入数据到utf8表,然后取出到gbk页面 首先, 这个set names x等价于SET character_se ...
- python从网络时间服务器获取并打印当前时间以及pip安装ntplib的一次体验
首先需要安装ntplib,科一通过pip安装. ubuntu下科一通过如下指令安装pip: $ sudo apt-get install python-pip 使用如下指令安装ntplib: $ su ...
- linux 时钟时间,用户CPU时间,系统CPU时间 .
之前看过几次这几个的概念,但还是老是记不住,干脆就直接写下来,以后方便看~ 所谓的时钟时间又叫做墙上时钟时间,它是进程运行的时钟总量,其值与系统中同时运行的进程数有关,不过一般在讨论时钟时间的时候都是 ...
- 以太坊(二)安装Solidity编译器
官方地址:https://solidity.readthedocs.io/en/develop/installing-solidity.html 推荐使用 remix 快速学习solidity ...
- soap,socket
Socket 接口是访问 Internet 使用得最广泛的方法. 如果你有一台刚配好TCP/IP协议的主机,其IP地址是202.120.127.201, 此时在另一台主机或同一台主机上执行ftp 20 ...
- 浅谈PHP面向对象编程(七、抽象类与接口)
7.0 抽象类与接口 当定义一个类时,常常需要定义一些方法来描述该类的行为特征.但有时这些方法的实现方式是无法确定的,此时就可以使用抽象类和接口. 抽象类和接口用于提高程序的灵活性.抽象类是一种特殊的 ...
- netcore中使用log4net日志
第一.控制台程序中使用log4net static void Main(string[] args) { ILoggerRepository repository = LoggerManager.C ...
- Oracle查看和修改连接数
1.查询数据库当前进程的连接数: select count(*) from v$process; 2.查看数据库当前会话的连接数: elect count(*) from v$sessio ...
- Less、Sass/Scss
一.Less.Sass/Scss是什么? 1.Less: 是一种动态样式语言. 对CSS赋予了动态语言的特性,如变量.继承.运算.函数. Less 既可以在客户端上运行 (支持IE 6+, Webki ...
- VMware Workstation 12 Pro(安装CentOS7)
之前安装了一版 Ubuntu 14.04版本,发现蛮不好用的,果断放弃,换上CentOS7版本(在远程服务器上的安装方式除了网络设置有差异,基本相同) VMware Workstation 12 Pr ...