求问下各位大神,怎么判断一个按照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的更多相关文章

  1. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  2. Construct BST from given preorder traversal

    Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...

  3. Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合

    给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 Given a binary tree, retur ...

  4. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  5. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  8. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. LintCode Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...

随机推荐

  1. FileResult,JavaScriptResult,JsonResult

    FileResult:可以响应任意文档的属性,包括二进制格式的数据,eg:图档,pdf,excel,zip,可以传入byte[],文档路径,Stream等不同的属性,让mvc将属性回传给客户端,除此之 ...

  2. cursor:pointer

    .cursor_hand{ cursor:pointer; }

  3. P2409 Y的积木

    luogu月赛 暴力dfs,估计过不了几个点,大概也就得30分左右? #include <bits/stdc++.h> using namespace std; const int max ...

  4. Python创建list和按照索引访问list

    Python创建list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.比如,列出班里所有同学的名字,就可以用一个list表示:>> ...

  5. Bluetooth数据包捕获

    目录 1. 前提 2. 开启功能 3. 抓包 这里介绍一种在Android上捕获蓝牙数据包的方法 1. 前提 首先你要有一部Android手机 然后你的Android系统版本要在4.4及以上 我没有做 ...

  6. Nginx的配置中与流量分发相关的配置规范:

    1.除首页外,其他页面都在某个目录中首页可以直接在根目录下,其他页面都要在根目录下的目录中.不同的location尽量使用第一个dir的模式进行区分,便于区分该流量是落在nginx本地,还是转发到后端 ...

  7. php 连接主从数据库

    本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的.这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议.封装的方式比较简单 ...

  8. 使用dnspod进行简单的HTTP dns解析(防劫持)

    https://www.dnspod.cn/httpdns/guide https://www.dnspod.cn/misc/D%2B免费版本接口说明.pdf 最简单的get接口去请求dnspod提供 ...

  9. hbase与mapreduce集成

    一:运行给定的案例 1.获取jar包里的方法 2.运行hbase自带的mapreduce程序 lib/hbase-server-0.98.6-hadoop2.jar 3.具体运行 4.运行一个小方法 ...

  10. RequestContextListener有什么用

    问题: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request att ...