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. linux之vi编辑器的基础命令

    1,假如要在这个php文件的phpinfo.php;之后加入一行,我们可以先按键盘的"a",光标就会跳转到之前绿色光标之后,也就是说,"a"是代表在当前光标之后 ...

  2. composer安装及使用说明和相关原理文档

    一.安装composer: 1.官方安装方法见https://getcomposer.org/download/   2.本人安装方法: ①先配好yum源(不会配置的见博客如何制作自己的yum源),我 ...

  3. jenkins 集成钉钉机器人

    最早做Jenkins发布完成以后通过邮件发送信息通知相关的联系人,发现邮件会受限于大家接收的设置,导致不能及时的看到相关的发布内容,公司使用钉钉做为公司内部的通讯工具,所以想通过Jenkins发布完成 ...

  4. liunx命令2

    查看系统和bios硬件时间 [root@localhost ~]# date '+%y- %m- %d %H:%M' 17- 05- 15 02:57[root@localhost ~]# date ...

  5. 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)

    16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...

  6. Android网络下载图片

    package net.learn2develop.Networking; import android.app.Activity; import android.os.Bundle; import ...

  7. java.util.Properties类 学习笔记

    学习目标:   1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路 ...

  8. jquery和vue对比

    1.jquery介绍:想必大家都用过jquery吧,这个曾经也是现在依然最流行的web前端js库,可是现在无论是国内还是国外他的使用率正在渐渐被其他的js库所代替,随着浏览器厂商对HTML5规范统一遵 ...

  9. oracle配置odbc数据源

    今天配置oracle数据源心得: 1.需安装oracle客户端,若校验报错,将杀毒软件全部退出之后再重新安装: 2.安装完成后,运行odbcad32(64位),在odbc界面可找到相应驱动: 3.客户 ...

  10. jQuery UI 入门之实用实例分享

    jQuery UI 入门 jQuery UI 简介 jQuery UI 是一个建立在 jQuery JavaScript 库上的小部件和交互库,您可以使用它创建高度交互的 Web 应用程序.无论您是创 ...