题意

给一棵二叉树,把它转化为字符串返回。转化字符串的要求如下:

1.  null 直接转化为  () ;(这个要求其实有点误导人~)

2. 子节点用 () 包裹起来;(这是我自己根据例子添加的要求)

3. 省略所有不影响 二叉树 与 字符串 之间 一对一 关系的 () ;

做题链接

代码

//JavaScript
var tree2str = function(t) {
if(t === null) return '';
if(t.left === null && t.right === null) return ''+t.val;
return '' + t.val + '(' + tree2str(t.left) + ')' + (t.right === null ? '' : '(' + tree2str(t.right) + ')');
};

简单的二叉树题目,一般我都期待能用简单的递归完成。

这道题起初我把问题想得太复杂,以为需要一个辅助函数。分析例子和答案之后才发现其实不用。

三种情况

三种情况分别对应上面三行代码

情况一:

if(t === null) return '';

这种情况究竟需要  return '';  还是  return '()'; 呢?

上面说过,LeetCode原题的描述有点误导人。

我自己的分析是: () 是父节点 用来包裹 子节点,以区分层级关系的。所以如果用递归,需要包裹括号的时候,也应该由父节点来执行包裹这个动作,不然不好分析,而且需要额外处理特殊情况。所以最好还是  return '';

情况二:

if(t.left === null && t.right === null) return ''+t.val;

这个情况相对简单,因为前面我们分析过了,最好由父节点来执行包裹括号的操作,所以这里我们的逻辑就比较简明。

情况三:

//当节点 t 不为 null,且它至少有一个子节点不为 null
return '' + t.val + '(' + tree2str(t.left) + ')' + (t.right === null ? '' : '(' + tree2str(t.right) + ')');

这种情况相对烧脑。因为题目的误导,一开始我想的很深,结果做出来,发现没有那么复杂。

例如上面的对应字符串为:  "1(2(4(6)))(3()(5(7)))" 。

做出来之后重新分析了一下,发现其实是这样的:

1. 一个 () 只包裹一个子树,所以其实当右子树是 null 的时候,可以直接省略掉右子树字符串(不会影响整体布局),当然左子树为 null 的时候不一定可以。

2. 在第三种情况下,   '(' + tree2str(t.left) + ')' 一定需要调用,为什么呢?因为如果左子树为 null ,则证明右子树不为 null (因为第三种情况是至少有一个子树不为  null ),在这种情况下,左子树一定要有一个空括号来占一个位置,才不会影响树的整体布局;而当左子树不为 null 时,废话不多说,当然要调用。

PS:英文分析链接

【刷题笔记】LeetCode 606. Construct String from Binary Tree的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  8. 606. Construct String from Binary Tree 从二叉树中构建字符串

    [抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...

  9. 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. lhgdialog.js弹出框

    官方学习网址: http://www.lhgdialog.com/ 个人认为它的样式不太好调,除此之外它也是一款实用的弹出框,专业的用来提示文字,消息,按钮添加function().ifame: 以下 ...

  2. 3.Ventuz Designer新建项目Demo

    Ventuz Designer新建项目Demo 1.打开ventuz,点Recent Projects>New Project,在弹出的界面填写具体项目信息,如下图: 图1.1 图1.2 2.在 ...

  3. Winform开发 如何为dataGridView 添加CheckBox列,并获取选中行

    //添加CheckBox列 DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn(); columncb.Heade ...

  4. MVC 返回json数据

    使用 return Json(pageList, JsonRequestBehavior.AllowGet); 返回object 使用 return Content(JsonConvert.Seria ...

  5. 如何把数值或者对象添加到ArrayList集合

    生成6个1~33之间的随机整数,添加到集合,并遍历 public class ArrayListDemo1 { public static void main(String[] args) { // ...

  6. MongoDB 学习笔记(七):主从复制与副本集

    一.主从复制 1.主从复制是一个简单的数据库同步备份的集群技术,如下图:要明确的知道主服务器与从服务器,且从服务器要明确的知道主服务器的存在. 2.在MongoDB中在启动数据库服务时,可以用mast ...

  7. print输出带颜色的方法详解

    书写格式:     开头部分:\033[显示方式;前景色;背景色m + 结尾部分:\033[0m      注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个:另外由于 ...

  8. Integer Intervals POJ - 1716_查分约束_

    Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...

  9. linux 编译网卡驱动

    将smsc7500网卡驱动拷贝到/drive/net/usb文件夹下 拷贝ioctl_7500.h  smsc7500usbnet.c smsc7500version.h smsclan7500.h ...

  10. python第一周:python初识、流程控制

    编译性语言:在将源代码编译完毕生成一个可执行文件后才能运行 解释性语言:在代码的运行期间进行编译 动态类型语言:在运行期间才去做数据检查的语言,也就是说在使用动态类型语言时不用指定数据类型 静态类型语 ...