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 ...
随机推荐
- Sphinx+MySQL5.1x+SphinxSE+mmseg
一.不停止mysql的情况下安装SphinxSE 1.确定mysql版本,下载对应源码包 此处下载5.1.69的mysql源码包 #wget ftp://ftp.ntu.edu.tw/pub/MySQ ...
- 使用fsck修复文件系统错误
1.问题描述 服务器maint_samba 由于服务器maint_samba (debian操作系统)没有正常关机,在重新启动过程中/dev/sdb1出现文件系统错误,需要手动使用fsck进行扫描 ...
- Deep Learning Tutorial
http://www.slideshare.net/tw_dsconf/ss-62245351?qid=c0f0f97a-6ca8-4df0-97e2-984452215ee7&v=& ...
- Lazarus中system.length说明
在system单元中我们有Length专门用来获取字符串宽度和数组宽度,下面例子来介绍他的功能. 定义: function Length( S: AStringType ):Integer; fu ...
- Mysql 安装问题排查方法
重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local MySQL server ...
- [转]C#之反射
前言 之所以要写这篇关于C#反射的随笔,起因有两个: 第一个是自己开发的网站需要用到 其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...
- ASP.NET一些公共方法commTools
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- 蓝牙4.0(包含BLE)简介
1. BLE (低功耗蓝牙)简介 国际蓝牙联盟( BT-SIG,TI 是 企业成员之一)通过的一个标准蓝牙无线协议. 主要的新特性是在蓝牙标准版本上添加了4.0 蓝牙规范 (2010 年6 月 ...
- centos apache安装和设置
分类: LINUX 安装方式:yum install httpdyum install mysql-serveryum install phpyum install php-mysql 一.WEB服务 ...
- asp.net实现关闭当前网页
asp.net实现关闭当前网页功能:Response.Write("<script>window.close();</script>");// 会弹出询问是 ...