[抄题]:

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

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 字符串的题判断空的条件是 == "",头一次见 注意下
  2. 先dfs, 后操作,所以没理解到位的事 都是对dfs的结果left right操作

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

  1. 检查函数返回值,不是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 + ")";
}
}

最新文章

  1. Chrome开发者工具不完全指南(一、基础功能篇)
  2. 关于STM32的FLASH操作【转载】
  3. laravel框架总结(三) -- 路径分析
  4. 更改Sublimetext3的主题文件,改变某些不喜欢的颜色
  5. EffectiveC#2--为你的常量选择readonly而不是const
  6. spring4——IOC之基于注解的依赖注入(DI )
  7. Leetcode_110_Balanced Binary Tree
  8. LFYZ-OJ ID: 1024 火车站
  9. php中excel以及cvs等导入以及导出
  10. ERROR: cannot launch node of type [turtlebot_teleop/turtlebot_teleop_key] 问题解决
  11. C# 之 @ Assembly
  12. 利用CCS3渐变实现条纹背景
  13. 在做销售录入界面时,如何使用dbgrid?(50分)
  14. Confluence 6 查看所有空间
  15. 图片 base64转byte[]
  16. Cisco & H3C 交换机 DHCP 中继
  17. markdown 常用语法格式
  18. SQL查询优化的一些建议
  19. iptable防火墙案例
  20. 使用Celery踩过的坑

热门文章

  1. 一次解决spark history server日志不见
  2. RedHat6.5用ISO配置yum源
  3. 使用FileZilla连接Linux
  4. 关于INTEL FPGA设计工具DSP Builder
  5. Tool:Visual Studio Code
  6. 实现Runnable接口和继承Thread类
  7. node启动appium.js
  8. OD 实验(十) - 对一个 VB 程序的逆向
  9. NIO buffer 缓冲区 API
  10. krpano之缩略图文本添加