[抄题]:

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:

  1. There will only be '('')''-' and '0' ~ '9' in the input string.
  2. An empty tree is represented by "" instead of "()".

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

注意下:取数可以多取几位,i+1位是数字时就继续i++

[思维问题]:

感觉我在背题:几天不背,功力全无。substring都忘了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. new TreeNode(Integer.valueOf(s.substring(j, i + 1)))字符串不能直接转node,需要转interger后再转node
  2. 一直往后移用的是while循环

[二刷]:

  1. new TreeNode(Integer.valueOf(s.substring(j, i + 1))) j的初始值是i,计算之后也应该更新为新的i

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

从i j中截取字符串, j应该跟随i更新

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

for (int i = 0, j = i; i < s.length(); i++, j = i) {
TreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode str2tree(String s) {
//corner case
if (s == null || s.length() == 0) return null; //initialization: stack
Stack<TreeNode> stack = new Stack<TreeNode>(); //for loop: new node, get substring and append
for (int i = 0, j = i; i < s.length(); i++, j = i) {
//get c
char c = s.charAt(i); //if c is )
if (c == ')') stack.pop();
else if ((c >= '0' && c <= '9') || (c == '-')) {
//continue
while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') i++;
//build new node
TreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
if (!stack.isEmpty()) {
TreeNode parent = stack.peek();
//get left and append
if (parent.left != null) {
parent.right = node;
}
else parent.left = node;
}
stack.push(node);
} } //return the last root
return stack.peek() == null ? null : stack.pop();
}
}

536. Construct Binary Tree from String 从括号字符串中构建二叉树的更多相关文章

  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. 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...

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

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

  4. LeetCode Construct Binary Tree from String

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-string/description/ 题目: You need to ...

  5. Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树

    根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...

  6. LeetCode 536----Construct Binary Tree from String

    536. Construct Binary Tree from String You need to construct a binary tree from a string consisting ...

  7. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  8. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  9. [LeetCode] 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 ...

随机推荐

  1. 用virtualenv建立独立虚拟环境 批量导入模块信息

    pip3 install virtualenv mkdir env/env1 source bin/activate pip3 freeze >requirements.txt or pipre ...

  2. 创建一个dynamics 365 CRM online plugin (三) - PostOperation

    上两节我们创建了一个 PreOperation的plugin 今天我们创建一个PostOpeartion的plugin和之前的plugin连接起来 当创建contact之后,我们要添加一个task给新 ...

  3. 3.2-1937 Problem D

    #include <stdio.h> ; }; }; int main(void) { int n; while(scanf("%d", &n) != EOF) ...

  4. 在flask框架中,对wtforms的SelectMultipleField的一个报错处理

    先粘贴代码: form.py文件: users = SelectMultipleField( label="请选择用户", validators=[ DataRequired(&q ...

  5. Code::Blocks 导入Makefile工程

    1)“File -> New -> Project”,选择“Empty Project”并创建. 2)选中 Project,右键,选择“Add files”,将 c/c++ 和 head ...

  6. 在Docker中监控Java应用程序的5个方法

    译者注:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.通常情况下,监控的主要目的在于:减少宕机 ...

  7. wordpress 插件Simple Social Buttons import处漏洞复现

    前言: 漏洞范围范围:simple socail buttons v2.0.4到v2.0.22之间的所有版本 利用条件,wordpress的普通用户 漏洞细节:该插件缺少权限的检查,非管理管权限执行管 ...

  8. 数据访问安全--数据库遮罩及断词 Data Masking & Tokenization

    现在大数据时代几乎无隐私,各政府部门各公司都要求实名制(动不动手机认证,身份证号码认证),但又无力确保数据安全,称为乱象. 其实在2011年,我们就接触过数据库遮罩断词产品,一个澳大利亚公司产品. 简 ...

  9. 理解Linux系统负荷load average

    理解Linux系统负荷   一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在 ...

  10. 403 Forbidden是什么意思?403 Forbidden错误解决方法

    大家平常在访问不同网站的时候,偶尔会遇到出现403 Forbidden错误的情况,浏览器会给出403 Forbidden错误提示.那么,403 forbidden是什么意思呢?出现403 Forbid ...