[抄题]:

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:

  1. Input: "4(2(3)(1))(6(5))"
  2. Output: return the tree root node representing the following tree:
  3.  
  4. 4
  5. / \
  6. 2 6
  7. / \ /
  8. 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()

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

[关键模板化代码]:

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

[其他解法]:

[Follow Up]:

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

[代码风格] :

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

[潜台词] :

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public TreeNode str2tree(String s) {
  12. //corner case
  13. if (s == null || s.length() == 0) return null;
  14.  
  15. //initialization: stack
  16. Stack<TreeNode> stack = new Stack<TreeNode>();
  17.  
  18. //for loop: new node, get substring and append
  19. for (int i = 0, j = i; i < s.length(); i++, j = i) {
  20. //get c
  21. char c = s.charAt(i);
  22.  
  23. //if c is )
  24. if (c == ')') stack.pop();
  25. else if ((c >= '0' && c <= '9') || (c == '-')) {
  26. //continue
  27. while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') i++;
  28. //build new node
  29. TreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
  30. if (!stack.isEmpty()) {
  31. TreeNode parent = stack.peek();
  32. //get left and append
  33. if (parent.left != null) {
  34. parent.right = node;
  35. }
  36. else parent.left = node;
  37. }
  38. stack.push(node);
  39. }
  40.  
  41. }
  42.  
  43. //return the last root
  44. return stack.peek() == null ? null : stack.pop();
  45. }
  46. }

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. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  2. Office常用技巧

    文章目录 大小写切换 把word里的自动编号转换为真实的文本 大小写切换 word中修改单词/句子的大小写:选中文字,按shift+F3,可在全大写.全小写.首字符大写间切换. 把word里的自动编号 ...

  3. node笔记汇总

    项目依赖分两种,一个就是普通的项目依赖比如bootstrap,还用一种只是开发阶段需要用的,这种属于开发依赖比如gulp,开发依赖最终记录在devDependencies节点里面 -          ...

  4. vue+elementui按需引入

    转载自以下网址,仅作备忘之用:https://www.cnblogs.com/lwj820876312/p/9169457.html 基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 ...

  5. [C#]typeof,Gettype()和is的区别

    typeof 参数是一个类型名称,比如你自己编写的一个类 GetType()是类的方法,继承自object,返回该实例的类型 is 用来检测实例的兼容性(是否可以相互转换) 例: class Anim ...

  6. verilog 除法器

    verilog 除法器:利用二进制的除法翻译过来的硬件电路 1.1 实现算法 基于减法的除法器的算法: 对于32的无符号除法,被除数a除以除数b,他们的商和余数一定不会超过32位.首先将a转换成高32 ...

  7. docker入门 什么是docker? 为什么使用docker?

    1.什么是docker? 轻量级操作系统虚拟化解决方案 2.为什么使用docker? 1.docker的启动是秒级的,比传统虚拟机快很多 2.资源利用率高,一台主机上可同时运行数千个docker容器 ...

  8. 执行webpack-dev-server时,提示端口被占用。

    执行webpack-dev-server时总出错,提示端口被占用.百度了很多答案都不能解决,最后找到了解决方案,如下: webpack-dev-server  --port 8088 使用以上命令修改 ...

  9. RobotFramework - AppiumLibrary 之元素定位

    一.介绍 AppiumLibrary 是 Robot Framework 的App测试库. 它使用Appium 与Android 和 iOS应用程序进行通信,类似于Selenium WebDriver ...

  10. [java,2017-05-16] java中清空StringBuffer的方法以及耗费时间比较

    java中清空StringBuffer的方法,我能想到的有4种: 1. buffer.setLength(0);  设置长度为0 2. buffer.delete(0, buffer.length() ...