[抄题]:

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 caseif (s == null || s.length() == 0) return null;//initialization: stackStack<TreeNode> stack = new Stack<TreeNode>();//for loop: new node, get substring and appendfor (int i = 0, j = i; i < s.length(); i++, j = i) {//get cchar c = s.charAt(i);//if c is )if (c == ')') stack.pop();else if ((c >= '0' && c <= '9') || (c == '-')) {//continuewhile (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') i++;//build new nodeTreeNode node = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));if (!stack.isEmpty()) {TreeNode parent = stack.peek();//get left and appendif (parent.left != null) {parent.right = node;}else parent.left = node;}stack.push(node);}}//return the last rootreturn stack.peek() == null ? null : stack.pop();}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/9612471.html

536. Construct Binary Tree from String 从括号字符串中构建二叉树相关推荐

  1. LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法

    题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...

  2. 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 ...

  3. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  4. [leetcode] Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  5. [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 t ...

  6. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  7. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  8. 【Construct Binary Tree from Inorder and Postorder Traversal】cpp

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  9. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

最新文章

  1. OSChina 周一乱弹 ——喝不到放心奶
  2. 中国版 Ubuntu Kylin 14.04 LTS 麒麟操作系统中文版发布下载 (Ubuntu天朝定制版)
  3. Jenkins+Ant+TestNG+Testlink自动化构建集成
  4. springboot加载外部xml_SpringBoot读取外部配置文件的方法
  5. 【深度学习】ICCV2021|性能优于何恺明团队MoCo v2,DetCo:为目标检测定制任务的对比学习...
  6. 景观分析工具:arcgis中patch analysis模块
  7. WebRTC促进跨平台指挥调度,触发安防应用新创意
  8. CSS中背景图片的坐标之使用说明及css中把所有背景图都放在一张图片上减少图片服务器的请求次数问题(转)...
  9. 计算机组成原理学习笔记——数据通路
  10. bugku never_give_up file_get_contents()有php://input漏洞 eregi \x00绕过
  11. SpringBoot+Vue项目的PDF导出及给PDF文件盖章的功能示例
  12. JSON的生成和解析
  13. 2篇SCI二区认定优秀博士!57万安家费+100万房补,浙江高校!
  14. 真正牛逼的人,都是极简主义者!!
  15. GEM5 模拟器简介
  16. [转贴]Debian的汉化步骤【转自http://www.linuxsir.org】
  17. 在mips64架构的国产系统中安装pyinstaller
  18. iTunes C盘占用空间太大 解决方案
  19. VS2019 C# MySQL 学生信息增删改查(二、改查(续前节))
  20. natapp邀请码,新用户购买域名可以享受9折优惠

热门文章

  1. vue-router 在项目中的使用
  2. ★自制社交网站等级称号
  3. Eclipse上安装springsource-tool-suite
  4. 二十四种设计模式:备忘录模式(Memento Pattern)
  5. 学习官方示例 - System.Frac: 返回小数部分
  6. [QTP] 描述性编程
  7. java集合类详细概述
  8. Java集合—List如何一边遍历,一边删除?
  9. 模拟分发扑克牌(python实现)
  10. Flutter入门:设置全局字体