你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。

空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。

示例 1:

输入:

二叉树: [1,2,3,4]

1

/ \

2      3

/

4

输出: "1(2(4))(3)"

解释: 原本将是“1(2(4)())(3())”, 在你省略所有不必要的空括号对之后, 它将是“1(2(4))(3)”。

示例 2:

输入:

二叉树: [1,2,3,null,4]

1

/  \

2      3

\ 4

输出: "1(2()(4))(3)" 解释: 和第一个示例相似, 除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
string res = "";
string tree2str(TreeNode* t) {
if (t == NULL)
return res;
GetAns(t);
return res;
} void GetAns(TreeNode* root)
{
if(root != NULL)
res += to_string(root->val);
if (root != NULL && (root->right != NULL || root->left != NULL))
{
res += '(';
GetAns(root->left);
res += ')';
}
if (root != NULL && root->right != NULL)
{
res += '(';
GetAns(root->right);
res += ')';
}
}
};

Leetcode606.Construct String from Binary Tree根据二叉树创建字符串的更多相关文章

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

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

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

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

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

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

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

  6. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

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

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

  8. 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. LeetCode 606 Construct String from Binary Tree 解题报告

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

随机推荐

  1. 我眼中javascript的这些年

    写了两年多的js了吧,一开始的目标并没有很学术,我只想安安静静做个很厉害的开发者.但是总是沉溺在一种语言里,会让人误以为这种语言很厉害,就像只在村子里混的话,我觉得我自己可以逆天,村外有人,编程世界也 ...

  2. WCF加密操作(包括证书和证书+帐号密码)

    WCF作为.net三大组件之一,伟大之处不用多说,但是其加密配置对于我这样的萌新来说还是颇有难度,因此将几天来的研究成果共享出来,与各位共勉~ 首先声明我的开发环境,Win10创意者更新 + Visu ...

  3. PAT甲级——【牛客A1005】

    题目描述 Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits ...

  4. rabbit mq 基础流程(转)

    从AMQP协议可以看出,MessageQueue.Exchange和Binding构成了AMQP协议的核心,下面我们就围绕这三个主要组件    从应用使用的角度全面的介绍如何利用Rabbit MQ构建 ...

  5. 07_springmvc校验

    一.概述 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验: 控制层conroller:校验页面请求的参数的合法性.在服务端控制层conrol ...

  6. Appium环境安装步骤 + 代码验证环境是否成功

    1.安装Microsoft .NET Framework 4.5 检测本机已安装的程序中,是否已经安装Microsoft .NET Framework 4.5及以上的版本. 如下图所示:   如果没有 ...

  7. python tkiner实现自动打包程序

    环境 python3.x 使用前请确保安装pyinstaller库 本程序还未完善,可以自行完善 若要使用加密,请自行安装cryptodome库 import tkinter as tk from t ...

  8. 利用zepto.js实现移动页面图片全屏滑动

    HTML <%-- Created by IntelliJ IDEA. User: fanso2o Date: 2017/2/28 Time: 16:09 To change this temp ...

  9. 廖雪峰Java11多线程编程-1线程的概念-2创建新线程

    Java语言内置多线程支持: 一个Java程序实际上是一个JVM进程 JVM用一个主线程来执行main()方法 在main()方法中又可以启动多个线程 1.创建新线程 1.1 方法一:使用Thread ...

  10. php 网页内容抓取

    最近抓的2个网站内容的代码 列表页抓取:第一种使用phpquery插件,可以快速获取,第二种它是api,所以直接获取 load_third("phpQuery.php"); /** ...