[抄题]:

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. RK3288 查看ddr频率

    转载请注明出处:https://www.cnblogs.com/lialong1st/p/8515135.html RK3288 查看 ddr 当前频率的方式有两种,第一种是通过 adb 查看,第二种 ...

  2. NFS搭建与配置

    NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致性 172.131.1.135  服务器端 1 ...

  3. 搭建基于hyperledger fabric的联盟社区(九) --检索状态数据库

    一.启动elasticsearch服务 官网下载压缩包解压,进入bin目录启动: ./elasticsearch 通过ip访问 localhost:9200,可以看到如下信息 { name: &quo ...

  4. cowboy的get和post的例子

    官方get和post的代码是有问题的,1.1下运行crash,这里修改了下,贴代码 创建工程 rebar-creator create-app testCowboy testCowboy_app.er ...

  5. k8s1.4.3安装实践记录(3)下载基础镜像

    下载基础镜像,因为Google被墙,所以我们用时速云中的镜像来tag docker pull index.tenxcloud.com/google_containers/pause-amd64:3.0 ...

  6. 读jQuery之二十(Deferred对象)--(转)

    原博文地址:http://www.cnblogs.com/snandy/archive/2012/12/19/2812935.html Deferred对象是由.Deferred构造的,.Deferr ...

  7. Java实现HTML转换为PDF的常见方法

    最近在自己的项目中需要动态生成融资单合同,这里需要把对应的html转换为对应的pdf融资合同.因此需要通过Java实现将HTML转PDF.自己之前没有接触过这一块的东西,所以上网查了一下,网上有很多的 ...

  8. (转) docker跨主机 macvlan 网络配置

    原文链接 https://github.com/alfredhuang211/study-docker-doc/blob/master/docker%E8%B7%A8%E4%B8%BB%E6%9C%B ...

  9. 第四章 Kubernetes 架构

    4.1 Master节点:Master是大脑,运行如下Daemon服务: API Server(kube-apiserver)      API server提供了HTTP/HTTPS RESTful ...

  10. B2B,ERP,SCM

    B2B: B2B电子商务平台是指一个市场的领域的一种,是企业对企业之间的营销关系.电子商务是现代B2B marketing的一种具体主要的表现形式.网商通过它将企业内部网,通过B2B网站与客户紧密结合 ...