[抄题]:

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 + ")";
}
}

606. Construct String from Binary Tree 从二叉树中构建字符串的更多相关文章

  1. 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 ...

  2. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  3. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  4. 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 ...

  5. 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  6. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  7. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  8. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  9. [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 ...

随机推荐

  1. hibernate规避SQL注入实例

    项目被检测出SQL注入,注入url如:http://127.0.0.1:8080/Test/wlf/getServiceInfo.html?province=%25E6%25B5%2599%25E6% ...

  2. matlab读写图片,读取图像序列,读取AVI视频

    介绍使用matlab读写图片,读取图像序列,读取AVI视频的方法: 一.读写图像 使用matlab读一幅图像,并另存 % Filename: ImageReadWrite clc; clear; i ...

  3. bzoj4764: 弹飞大爷

    Description 自从WC退役以来,大爷是越来越懒惰了.为了帮助他活动筋骨,也是受到了弹飞绵羊一题的启发,机房的小伙伴们 决定齐心合力构造一个下面这样的序列.这个序列共有N项,每项都代表了一个小 ...

  4. SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo

    一. 搭建maven项目  1. 新建maven项目,选择maven Project --> Next 2. 勾选 Create a simple project --> Next 3. ...

  5. java代码---------比较随机数的大小---我搞不懂啊

    总结:不习惯你在或不在的时候,赶紧走吧 package com.mmm; import java.util.Random; public class wW { public static double ...

  6. python算两个时间之间的天数,将天数转成int型

    import time import datetime #计算两个日期相差天数,自定义函数名,和两个日期的变量名. def Caltime(date1,date2): #%Y-%m-%d为日期格式,其 ...

  7. mysql-5.6.17编译安装和常见问题

    mysql-5.6.17编译安装和常见问题 以前用的是MySQL的5.1版本,且使用的是免编译的包,安装简单.最近换了5.6版本的MySQL,安装过程出现了不少问题. 1. 卸载原来版本的MySQL ...

  8. jredis 客户端 使用

    redis学习及实践3---Jedis.JedisPool.Jedis分布式实例介绍 Java中使用Jedis操作Redis Redis客户端:Jedis

  9. thinkphp如果表名有下划线需要用Model

    模型命名规范 ThinkPHP 对数据库的表名和模型类的命名遵循一定的规范.首先数据库的表名和字段全部采用小写形式,模型类的命名规则是除去表前缀的数据表名称,并且首字母大写,然后加上模型类的后缀定义. ...

  10. Mybatis内部原理与插件原理

    Mybatis的运行分为两大问题,第一部分是读取配置文件保存在Configuration对象中,用以创建SqlSessionFactory,第二部分是SqlSession的执行过程.相对而言SqlSe ...