题目:

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. 实现统计 android手机 CPU使用率

    # -*- coding:utf-8 -*- ''' Created on Sep 10, 2018 @author: SaShuangYiBing ''' import subprocess imp ...

  2. FreeChart柱状图中如何取消柱子的倒影

    JFreeChart柱状图中如何取消柱子的倒影,让柱子显示为一个平面图 Render 该怎么设置呢? 问题补充:已解决 intervalBarRender.setShadowVisible(false ...

  3. Swift: Associated Types--为什么协议使用关联类型而不是泛型

    关联类型的形式为类型的引用进而进行约束提供了条件: 同时能够简化语法形式. Swift: Associated Types http://www.russbishop.net/swift-associ ...

  4. Scala学习之路 (四)Scala的数组、映射、元组、集合

    一.数组 1.定长数组和变长数组 import scala.collection.mutable.ArrayBuffer object TestScala { def main(args: Array ...

  5. day2-课堂笔记

    #面向对象 函数=方法 系统内建函数:len().id() 对象函数

  6. Jenkins忘记密码解决办法

    1.进入 如果安装的war包,路劲如下: C:\Users\LENOVO\.jenkins\ 2.打开config.xml  ->将useSecurity设置为false 3.进入系统管理的管理 ...

  7. OpenCV——距离变换与分水岭算法的(图像分割)

    C++: void distanceTransform(InputArray src, OutputArray dst, int distanceType, int maskSize) 参数详解: I ...

  8. Vue表单输入绑定(文本框和复选框)

    文本框 <!DOCTYPE html><html>    <head>        <meta charset="utf-8">  ...

  9. Java 包(package)

    为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. 1.包的作用 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2.如同文件夹一样,包也采用了树形目录的存储方式 ...

  10. $\mathcal{Friends' \ \ Links}$友情链接

    \(\mathcal{JuLao \ \& \ \ Dalao}\) \(\_rqy\) \(\_stdcall\) 并(吊)肩(锤)奋(死)斗(我)的\(Oier\) 王旭 苑骏康 张梓淳 ...