G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如
A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示
9
/ \
3 2
/ \ \
4 1 6
B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树
我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后就把"n##"改写成"#",也就是把找到的那个末端的子树想想成null,最后字符串变成"#"的就是valid,否则就invalid
比如 A) 9 3 "4 # #" "1 # #" 2 # "6 # #" ---> 9 3 # # 2 # # ---> 9 "3 # #" "2 # #" ---> 9 # # ---> "9 # #" ---> "#"
B) 9 3 "4 # #" "1 # #" ---> 9 3 # # ---> 9 "3 # #" ---> 9 # (没有"n##"结构了,return false)
也可用stack来这样做,当前如果是#,stack peek如果也是#且size>=2,就pop两次,且再check当前这个#
package PreorderValidSerialized;
import java.util.*; public class Solution {
public boolean check(String str) {
String[] strs = str.split(" ");
Stack<String> stack = new Stack<String>();
for (int i=0; i<strs.length; i++) {
if (stack.size()>=2 && strs[i].equals("#") && stack.peek().equals("#")) {
stack.pop();
stack.pop();
i--;
}
else stack.push(strs[i]);
}
if (stack.size()==1 && stack.peek().equals("#")) return true;
else return false;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
boolean res = sol.check("9 3 4 # # 1 # # 2 # 6 # # #");
if (res) System.out.println("true");
else System.out.println("false");
} }
G面经Prepare: Valid Preorder traversal serialized String的更多相关文章
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合
给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 Given a binary tree, retur ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LintCode Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...
随机推荐
- LR中的C语言问题
今天在调试LR的脚本(C)时遇到了几个甚是头痛得问题,下面简单总结下: 1.首先LR中的C编译器遵循C90标准,规定在一个函数中,变量定义必须放在所有的执行语句之前!一旦在运行语句之间再有定义的话,会 ...
- LR性能测试脚本增强与调试
脚本增强与调试 一般来说,使用LR的Vugen录制的脚本并不能直接用于测试,需要对脚本进行各方面的增强,主要包括添加注释.关联.检查点.事务.参数化.日志输出等.下面结合刚完成的一个web项目性能测试 ...
- cluster analysis in data mining
https://en.wikipedia.org/wiki/K-means_clustering k-means clustering is a method of vector quantizati ...
- git 第一次初始化
Command line instructions Git global setup git config --global user.name "{名字}({工号})" git ...
- ArcGIS API for Silverlight实现地图测距功能
原文:ArcGIS API for Silverlight实现地图测距功能 问题:如何实现地图测距功能? 地图工具栏 <Grid x:Name="gToolMenu" Hei ...
- Ubuntu 14.04安装OpenCV 3.1
从OpenCV官网上下载OpenCV官网上下载OpenCV的未编译源代码: 点击这里 国内很多网络打开OpenCV官网速度缓慢,可以点击如下地址直接从GitHub上下载OpenCV 3.1的源代码 下 ...
- 提取日志中的json请求发送到另外一台机器
将日志中的json请求提取,并且发送到另外一个机器上: for i in ` cat impression.log.2016-04-08-10 |awk -F"\t" ' {pri ...
- Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager.responseSerializer.acceptab ...
- C++经典编程题#1:含k个3的数
总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...
- iOS基础
1.属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用? readwrite 是可读可写特性:需要生成getter方法和s ...