536. 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"()".
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
注意下:取数可以多取几位,i+1位是数字时就继续i++
[思维问题]:
感觉我在背题:几天不背,功力全无。substring都忘了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
new TreeNode(Integer.valueOf(s.substring(j, i + 1)))字符串不能直接转node,需要转interger后再转node- 一直往后移用的是while循环
[二刷]:
- 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 从括号字符串中构建二叉树的更多相关文章
- [LeetCode] 536. Construct Binary Tree from String 从字符串创建二叉树
You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...
- 【LeetCode】536. Construct Binary Tree from String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计字符串出现的次数 日期 题目地址:https:// ...
- [LeetCode] Construct Binary Tree from String 从字符串创建二叉树
You need to construct a binary tree from a string consisting of parenthesis and integers. The whole ...
- LeetCode Construct Binary Tree from String
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-string/description/ 题目: You need to ...
- Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15, ...
- LeetCode 536----Construct Binary Tree from String
536. Construct Binary Tree from String You need to construct a binary tree from a string consisting ...
- 【题解二连发】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 ...
- 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 ...
- [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 ...
随机推荐
- python中的列表的嵌套与转换
第一种方法:这行代码的for循环的意识,是先将matrix列表中的每行的第一个元素拿出. matrix =[[1,2,3,4],[5,6,7,8],[9,10,11,12]] transposed_r ...
- 新系统centos7重启网络报错
场景: 在不知名云上新弄的centos7,改了IP之后启动不起来,使用systemctl status network查看结果如下: 排查过程: 1)NetworkManager是否关 ...
- 2、php中字符串单引号好和双引号的区别
使用单引号和双引号的主要区别是:单引号定义的字符串中出现的变量和转义序列不会被变量的值代替,而双引号中使用变量名会显示该变量的值.
- maya中的顶点平滑算法(vertex smooth algorithm)
继上文继续写.有了顶点迭代器之后就可以利用MItMeshVertex类的getConnectedVertices方法来获取相连点并代入平滑算法. 选择什么样的平滑算法呢?本人比较懒,直接打开了计算机图 ...
- selenium基础用法(爬虫)
初识selenium selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏 ...
- Windows系统中监控文件复制操作的几种方式
http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...
- Android之listview添加数据篇
一.ListView: 1. ListView通常有两个职责: 1.向布局填充数据 2.处理选择点击等操作 2.ListView的创建需要3个元素: 1. ListView中的每一列的View. 2. ...
- 使用docker查看jvm状态,在docker中使用jmap,jstat
Docker中查看JVM的信息: 1. 列出docker容器:docker ps 2. 标准输入和关联终端:docker exec -it 容器ID bash 3. 查找出j ...
- 源码:Java集合源码之:数组与链表(一)
数组和链表是数据结构中最基本的部分. 数组 在java中,数组定义为一种基本类型,其可以通过下标获取到对应位置的数据.那么这种结构的数据,在内存中是怎么存放的呢? 数组在内存中是一段连续的存储单元,每 ...
- 4、Zookeeper简单介绍
一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技术.那么什么是分布式协调技术?那么我来告诉大家,其实分布式协调技术 主要用来解决分布式环境当中多个进程之间的 ...