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 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++)的更多相关文章
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- 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 ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- 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 ...
- 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
随机推荐
- css常见知识点
1.内核区分 希望某一个浏览器能一统江湖 -ms-transform:rotate(7deg); //-ms代表ie内核识别码 -moz-transform:rotate(7deg); //-moz代 ...
- 团队作业7——第二次项目冲刺(Beta版本)day1
项目成员: 曾海明(组长):201421122036 于波(组员):201421122058 蓝朝浩(组员):201421122048 王珏 (组员):201421122057 叶赐红(组员):20 ...
- python3中启动 uiautomator2viewer时报错解决办法
事先说明:我电脑里同时了安装了 python2和python3,启动对应版本时分别带上版本号,如何设置共存请参考:python2与python3共存 打开命令窗口,启动weditor : python ...
- 【Android自动化】Subprocess.check_output()简单用法
# -*- coding:utf-8 -*- import os import sys import subprocess from uiautomator import device as d cm ...
- 关于MVC开发时,无法找到area的问题记录
解决方法: 检查area=admin 的dll是否生成,一般都是admin域生成dll导致
- 3、爬虫之selenium模块
一 selenium模块 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. selenium最初是一个自动化测试工具, ...
- 网页loading GIF图片(加载)
http://www.lanrentuku.com/gif/a/loading.html
- Thinkpad T420 调节声音进度条显示
重装了系统,进度条没了. 1.安装热键驱动:http://think.lenovo.com.cn/support/driver/driversdownlist.aspx?yt=pt&categ ...
- lsof |grep deleted;du -sh / ;df -h;
有台机器磁盘满了: 进程端口都正常,存活:但是页面却完全打不开了: 日志爆满:删除日志后: 在根上 du -sh * 然后 df -h 发现差别太大了: du -sh * / 才不足7G: df -h ...
- RedHat Enterprise Linux 6.4使用Centos 6的yum源问题
RedHat Enterprise Linux 6.4使用Centos 6的yum源问题 作为一名新手,学习Linux已经一个月了,其间遇到了不少问题,而今天笔者遇到的问题是 #yum install ...