【LeetCode】536. Construct Binary Tree from String 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
题目地址:https://leetcode-cn.com/problems/construct-binary-tree-from-string/
题目描述
You need to construct a binary tree from a string consisting of parenthesis and integers.
The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root’s value and a pair of parenthesis contains a child binary tree with the same structure.
You always start to construct the left child node of the parent first if it exists.
Example:
Input: “4(2(3)(1))(6(5))”
Output: return the tree root node representing the following tree:
4
/ \
2 6
/ \ /
3 1 5
Note:
There will only be ‘(’, ‘)’, ‘-’ and ‘0’ ~ ‘9’ in the input string.
An empty tree is represented by “” instead of “()”.
题目大意
判断一个多边形是不是凸多边形。
解题方法
统计字符串出现的次数
看到括号匹配,大部分做法当然是用栈来解决,其实可以直接用统计的方法完成。
即从左向右统计括号出现的次数count,如果是'('
则增加1,如果是')'
则减小1,如果count == 0了,说明已经完成了一个括号匹配。
这个题中的格式是root(left)(right)
,最多只会出现两个完美匹配的外部括号,分别找出两个左括号的位置first和second,然后使用剪切字符串并递归生成左右孩子。
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* str2tree(string s) {
if (s.empty()) return nullptr;
TreeNode* root = new TreeNode(s[0]);
int cnt = count(s.begin(), s.end(), '(');
int first = -1;
int second = -1;
int count = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
count ++;
if (count == 1) {
if (first == -1)
first = i;
else
second = i;
}
} else if (s[i] == ')') {
count --;
}
}
if (first == -1) {
root->val = atoi(s.c_str());
} else if (second == -1) {
root->val = atoi(s.substr(0, first).c_str());
root->left = str2tree(s.substr(first + 1, s.size() - first));
} else {
root->val = atoi(s.substr(0, first).c_str());
root->left = str2tree(s.substr(first + 1, second - first));
root->right = str2tree(s.substr(second + 1, s.size() - second));
}
return root;
}
};
日期
2019 年 9 月 20 日 —— 是选择中国互联网式加班?还是外企式养生?
【LeetCode】536. Construct Binary Tree from String 解题报告(C++)的更多相关文章
- [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...
- 536. Construct Binary Tree from String 从括号字符串中构建二叉树
[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- 【5】蛋白质组学鉴定定量软件之PD
目录 1.简介 2.安装与配置 3.分析流程 4.结果 1.简介 PD全称Proteome Discoverer,是ThermoFisher在2008年推出的商业Windows软件,没错,收费,还不菲 ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- 电脑盘符为什么从C盘开始?A盘和B盘去哪了?
虽然我们几乎每天都在跟电脑打交道,但是不知道大家有没有过疑惑,为什么电脑盘符是从C盘开始命名呢?有没有A盘和B盘??A盘和B盘去哪了??? 其实,A盘和B盘是真实存在的. 在早期的DOS时代,计算机的 ...
- 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用
7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...
- LeetCode两数之和
LeetCode 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...
- 剑指 Offer 10- I. 斐波那契数列
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N)).斐波那契数列的定义如下: F(0) = 0, F(1) = 1F(N) = F(N - 1) + F(N ...
- Linux下部署Java项目(jetty作为容器)常用脚本命令
startup.sh #!/bin/bash echo $(basename $(pwd)) "jetty started" cd jetty nohup java -Xmx8g ...
- 添加用户的jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %><!-- H ...
- 2.VUEJS-安装
Vue.js 安装 1.独立版本 我们可以在 Vue.js 的官网上直接下载 vue.min.js 并用 <script> 标签引入. 2.使用 CDN 方法 以下推荐国外比较稳定的两个 ...
- 【Linux】【RedHat】下载 安装 注册
RedHat 下载 安装 注册 记录 因为找入口太麻烦了,所以写了篇博文记录下来大致入口@萌狼蓝天 注册 点击进入注册地址(https://www.redhat.com/wapps/ugc/regis ...