【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意
给一棵二叉树,把它转化为字符串返回。转化字符串的要求如下:
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的更多相关文章
- 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 ...
- 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根据二叉树创建字符串 (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 解题报告(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 ...
- 606. Construct String from Binary Tree 从二叉树中构建字符串
[抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
随机推荐
- jquery选择器(可见对象,不可见对象) +判断,对象(逆序)
//可见对象: $("li:visible ") //可见对象下的 隐藏对象 $("li:visible [type='hidden']") //获得 可见 的 ...
- [原创]一道基本ACM试题的启示——多个测试用例的输入问题。
Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数 ...
- vue-留言板-bootstrap
最近看完入门API,看完视频自己写了个留言板,因为主要是学习vue,所以就复习了一下bootstrap,布局更简单,先看看样式吧. 简单清晰的布局,先说一下功能, 1.输入用户名密码点击提交放入表格 ...
- 杭电2060WA
#include<stdio.h> int main() { int n,num,p,q,i,a[]={2,3,4,5,6,7}; scanf("%d",&n) ...
- Javascript阻止表单提交
Javascript阻止表单提交 Html 1.<form name="loginForm" action="login.aspx" method=&qu ...
- vc++创建窗体
//创建窗口,一般分为四步 /*1 WinMain函数的定义 2 创建一个窗口 3 编写消息循环 4 编写窗口过程函数*/ #include <windows.h> //包含windows ...
- BZOJ1143: [CTSC2008]祭祀river 网络流_Floyd_最大独立集
Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组 ...
- MooFest 树状数组 + 前缀和
比较友好的数据结构题 建议读者好好思考思考--. 可以分析出与前缀和的关系, 之后就愉快的套起树状数组辣 #include <cstdio> #include<queue> # ...
- 死磕itchat源码--content.py
content.py中定义了接受消息的类型,即,用于注册消息函数时的参数类型.源码如下: TEXT = 'Text' MAP = 'Map' CARD = 'Card' NOTE = 'Note' S ...
- 一个很好的JS,ASP二级下拉框联动。
在我们制作网站会员注册信息时,一般会涉及到填写自己所在省/市,如果用input或textarea做成填写形式不太理想.所以大部分网站都会选择联动下来列表形式,做起来也不算很复杂,同时看上去也很轻松. ...