题目:

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.

分析:

给定一颗二叉树,按要求创建字符串。

我们可以发现,一个节点如果有左右节点的话,是要用()括起来左右节点,如果left存在,righ不存在,可以省略掉右边的括号,如果left不存在,right存在,左括号是不能省略的。

程序:

class Solution {
public:
string tree2str(TreeNode* t) {
string s = "";
if(t == nullptr) return s;
if(!t->left && !t->right)
return s + to_string(t->val);
if(t->right == nullptr)
return s + to_string(t->val) + "(" + tree2str(t->left) + ")";
return s + to_string(t->val) + "(" + tree2str(t->left) + ")("+ tree2str(t->right) + ")";
}
};

LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)的更多相关文章

  1. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  2. Leetcode606.Construct String from Binary Tree根据二叉树创建字符串

    你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...

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

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

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

  5. 【刷题笔记】LeetCode 606. Construct String from Binary Tree

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

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

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

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

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

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

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

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

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

随机推荐

  1. git bash安装和基本设置

    Part1:安装步骤 1.下载地址: https://git-for-windows.github.io/ 2.下载完成后安装,安装路径自己选择,其他的选项参照下图: 其他的一步一步往下即可,最后Fi ...

  2. Oracle物化视图的创建及使用(一

    Oracle物化视图的创建及使用 http://blog.csdn.net/tegwy/article/details/8935058 先看简单创建语句: create   materialized  ...

  3. 声明式编程:程序=数据+逻辑(what)+算法(控制+计算)

    接口:what: 实现:算法:指令: 编程语言中,凡是不涉及到算法的部分,都可以认为是声明式编程. 命令式编程可以与算法划等号:算法要求严格的计算逻辑和控制,是实施细节的精准描述: 命令式编程与声明式 ...

  4. Drool实战系列(二)之eclipse安装drools插件

    这里演示是drools7.5.0,大家可以根据自己需要安装不同的drools版本 drools安装地址: http://download.jboss.org/drools/release/ 一. 二. ...

  5. 【转】Android Camera 相机开发详解

    在Android 5.0(SDK 21)中,Google使用Camera2替代了Camera接口.Camera2在接口和架构上做了巨大的变动, 但是基于众所周知的原因,我们还必须基于 Android ...

  6. jupyter中添加conda环境

    安装完Anaconda利用conda创建了虚拟环境,但是启动jupyter notebook之后却找不到虚拟环境. 实际上是由于在虚拟环境下缺少kernel.json文件,解决方法如下: 首先安装ip ...

  7. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  8. 所做更改会影响共用模板Normal.dotm。是否保存此更改

    最近安装了Office 2010版本,但是发现个问题,每次在关闭word 2010时,都会提示所做更改会影响共用模板Normal.dotm …… 确实是烦恼,每次都需要点击是否保存,于是我在仔细研究了 ...

  9. 树上三角形 BZOJ3251

    分析: 模拟赛T3,其实很水,当时出于某些原因,没有去写这道题... len>46必定有解 为了满足不是三角形,那么斐波那契数列是最优选择,而斐波那契数列的第46项超过了2^31-1,所以超过4 ...

  10. Python基础之公共方法

    公共方法:就是列表,元组,字典,字符串能共同使用的方法: Python内置函数 内置函数罗列 函数 描述 备注 len(item) 计算容器中的元素个数 del(item) 删除变量 del有两种方法 ...