题目地址: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++)的更多相关文章

  1. [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树

    You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...

  2. 536. Construct Binary Tree from String 从括号字符串中构建二叉树

    [抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  4. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  5. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  7. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  8. (二叉树 递归) 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 ...

  9. (二叉树 递归) 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 ...

随机推荐

  1. SQL-增、删、改操作

    #查看表 select * from `竟企区域数据分析` #在表第一列新增名为"年月"的列alter table `竟企区域数据分析` add column 年月 varchar ...

  2. .NET SAAS 架构与设计 -SqlSugar ORM

    1.数据库设计 常用的Saas分库分为2种类型的库 1.1 基础信息库 主要存组织架构 .权限.字典.用户等 公共信息 性能优化:因为基础信息库是共享的,所以我们可以使用 读写分离,或者二级缓存来进行 ...

  3. java Random()用法

    1.random.nextInt() random.nextIn()的作用是随机生成一个int类型,因为int 的取值范围是 -2147483648--2147483647 ,所以生成的数也是处于这个 ...

  4. 日常Java 2021/11/17

    应用程序转换成Applet 将图形化的Java应用程序(是指,使用AWT的应用程序和使用java程序启动器启动的程序)转换成嵌入在web页面里的applet是很简单的.下面是将应用程序转换成.Appl ...

  5. act.四级

    act的词源是do, 干着或干了的事情也可以叫act.action: doing sth; act: n. action, v. do; activity: busy, energetic, or占据 ...

  6. midi的一些概念,包括一些音乐的概念

    参考:http://www.yueqixuexi.com/jita/20180918205363.html https://blog.csdn.net/meicheng777/article/deta ...

  7. LoadRunner中怎么设置密码参数化与用户名关联

    对密码参数化时从parameter里的"Select next row"列表中选择Same Line As这一选项,意思就是每一个密码参数化取值与对应行的用户名关联起来了

  8. dom4j解析XML学习

    原理:把dom与SAX进行了封装 优点:JDOM的一个智能分支.扩充了其灵活性增加了一些额外的功能. package com.dom4j.xml; import java.io.FileNotFoun ...

  9. 【编程思想】【设计模式】【创建模式creational】原形模式Prototype

    Python版 https://github.com/faif/python-patterns/blob/master/creational/prototype.py #!/usr/bin/env p ...

  10. 【Linux】【Basis】文件

    refer to: https://en.wikipedia.org/wiki/POSIX refer to: https://en.wikipedia.org/wiki/Unix_file_type ...