Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
示例 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根据二叉树创建字符串的更多相关文章
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 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 ...
- 606. Construct String from Binary Tree 从二叉树中构建字符串
[抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【Leetcode_easy】606. Construct String from Binary Tree
problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...
- [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 ...
- 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 ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
随机推荐
- Android之selector选择器的使用
1.selector简介 selector中文的意思选择器,在Android中常常用来作组件的背景,实现组件在不同状态下不同的背景颜色或图片的变换.使用十分方便.主要是用来改变ListView和But ...
- Netty设置高低水位
Configure high and low write watermarks Server ServerBootstrap bootstrap = new ServerBootstrap(); ...
- IDEA中log4j 无法输出到本地,properties配置无效问题。
log4j添加以后无法输出日志信息,经检查(按以下顺序): 1.jar包导入正常 2.log4j.properties配置文件正常 3.logger.info可以输出,但是properties文件无效 ...
- 基于用户的最近邻协同过滤算法(MovieLens数据集)
基于用户的最近邻算法(User-Based Neighbor Algorithms),是一种非概率性的协同过滤算法,也是推荐系统中最最古老,最著名的算法. 我们称那些兴趣相似的用户为邻居,如果用户 ...
- mahout in Action2.2-给用户推荐图书(3)-评价推荐系统
推荐系统引擎是一个工具,一种回答问题的手段,"对用户来讲什么是最好的推荐?",在研究回答的前先研究一下这个问题.一个好的推荐的准确含义是什么?如何知道推荐系统是如何生成推荐的?下面 ...
- [Ceoi2007]Royaltreasury
#1945. [Ceoi2007]Royaltreasury Online Judge:Bzoj-1945 Label:树形Dp,高精度 题目描述 在很久很久以前的一个王国里,王国的财产开始变得越来越 ...
- Windows API 第14篇 DeleteAndRenameFile
函数定义:BOOL DeleteAndRenameFile( LPCWSTR lpszDestFile, ...
- UDP和TCP的区别?
区别总结: 1.TCP面向连接,UDP的面向无连接的,即发送数据之前不需要建立简介. 2.TCP提供可靠的数据传输,有发送应答机制,超时重传机制,错误校验机制,流量控制机制保证传输的安全,不丢失,不重 ...
- MaxCompute问答整理之8月
本文是基于对MaxCompute产品的学习进度,再结合开发者社区里面的一些问题,进而整理成文.希望对大家有所帮助. 问题一.通过数据源数据增量同步后,如何查看某一条数据具体被同步到MaxCompute ...
- Python-基本文件处理
目录 文件的类型 什么是文件? 文件的分类 文件的打开与关闭 文件处理的三个步骤 使用方式 爬虫 requests库的使用 文件的类型 什么是文件? 一堆.py/.txt 存储着文字信息文件, 文件的 ...