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.

思路:

递归先序遍历二叉树,注意区分左子树和右子树是否为空的情况。

void pre(TreeNode* t,string& str)
{
if(t!=NULL)
{
str+=to_string(t->val);
if(t->left!=NULL || t->right!=NULL)
{
str+="(";
pre(t->left,str);
str+=")";
if(t->right!=NULL)
{
str+="(";
pre(t->right,str);
str+=")";
}
}
}
}
string tree2str(TreeNode* t)
{
string str="";
if(t==NULL) return str;
pre(t,str);
return str;
}

[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. 【刷题笔记】LeetCode 606. Construct String from Binary Tree

    题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1.  null 直接转化为  () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...

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

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

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

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

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

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

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

  9. Python 解LeetCode:606 Construct String from Binary Tree

    题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...

  10. 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. js算法集合(一) 水仙花数 及拓展(自幂数的判断)

    js算法集合(一) ★ 最近有些朋友跟我说对js中的一些算法感到很迷惑,知道这个算法到底是怎么回事,但是就是不会用代码把它写出来,这里我跟大家分享一下做水仙花数的算法的思路,并对其扩展到自幂数的算法, ...

  2. 【JAVAWEB学习笔记】16_session&cookie

    会话技术Cookie&Session 学习目标 案例一.记录用户的上次访问时间---cookie 案例二.实现验证码的校验----session 一.会话技术简介 1.存储客户端的状态 由一个 ...

  3. Spark实战之读写HBase

    1 配置 1.1 开发环境: HBase:hbase-1.0.0-cdh5.4.5.tar.gz Hadoop:hadoop-2.6.0-cdh5.4.5.tar.gz ZooKeeper:zooke ...

  4. Oracle 12C 新特性之 恢复表

    RMAN的表级和表分区级恢复应用场景:1.You need to recover a very small number of tables to a particular point in time ...

  5. RGB565的理解

    一个彩色图像由R G B三个分量组成,一个RGB565的每一个像素点数据为2Byte,即16位,那么从名字上就可看出来这16位中,高5位为R分量,中间6位为G分量,低5位为B分量. 下面做了一个实验, ...

  6. Customer segmentation – LifeCycle Grids, CLV and CAC with R(转)

    We studied a very powerful approach for customer segmentation in the previous post, which is based o ...

  7. HBuilder 安装使用教程

    前段时间朋友让我帮忙打包一个 IPA 文件(使用 HTML5 开发的 Web 应用),了解到 HBuilder 这款 H5 开发神器.之前一直使用 WebStorm 开发 H5,闲来无事也学习下 HB ...

  8. rownum基本用法

    对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...

  9. react 热替换 ([HMR])

    react 热替换 ([HMR]) 热替换好多地方可以用到,目前比较流行的用法是搭配React和webpack实现在不刷新页面的情况下对模块的增删改.在给项目添加热替换功能的时候,可以说是踩了各种坑, ...

  10. laravel 服务容器实现原理

    前言 通过实现laravel 框架功能,以便深入理解laravel框架的先进思想. 什么是服务容器 服务容器是用来管理类依赖与运行依赖注入的工具.Laravel框架中就是使用服务容器来实现 ** 控制 ...