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 ...
随机推荐
- iis发布----在XP中发布高版本web遇到问题总结
解决方法是之前在网上看到的,总结一下. 我在vs2010中做的web网页,放到XP系统中发布是遇到问题,XP中的iis版本是iis. 提示.net framework版本上的问题. 1.在网站属性中把 ...
- Go Example--defer
package main import ( "fmt" "os" ) func main() { f := createFile("/tmp/defe ...
- https://blog.csdn.net/doegoo/article/details/50749817
因为使用DiscuzX3.2进行系统的整合后,因为只是想在原J2EE的系统上增加论坛功能,而且J2EE中已经有一套用户的注册认证的体系,所以不需要在Discuz的系统中去注册以及登录功能,而是通过在J ...
- visual studio2017----编码和行尾
在 Visual Studio 中,以下字符将解释为换行符: CR LF:回车符 + 换行符,Unicode 字符 000D + 000A LF:换行符,Unicode 字符 000A NEL:下一行 ...
- Linux双线双网卡双IP双网关设置方法
机房上架了一台测试机,系统是Ubuntu 9.04 X64的系统,母机IBM X336机器.用户需求是双线,故采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好系统后配置好IP发现联通地址和电信 ...
- JVM的基本结构及其各部分详解(一)
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...
- Centos7更改yum源
每次都要百度一番,还不如自己做个记录,简单粗暴,哈哈哈哈 cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.old wget http: ...
- VUE打包上线优化
1.将vue vue-router vuex 尽量使用CDN externals: { 'vue':'Vue', 'vue-router':'VueRouter', 'vuex':'Vuex', 'a ...
- linux git 保存用户名和密码
一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...
- ZooKeeper和CAP理论及一致性原则
一.CAP理论概述CAP理论告诉我们,一个分布式系统不可能同时满足以下三种 一致性(C:Consistency)可用性(A:Available)分区容错性(P:Partition Tolerance) ...