One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.

     _9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #

For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.

Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.

Each comma separated value in the string must be either an integer or a character '#' representing null pointer.

You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".

Example 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
Return true

Example 2:
"1,#"
Return false

Example 3:
"9,#,#,1"
Return false

代码如下:(方法一:利用树节点的 初度=入度)

 public class Solution {
public boolean isValidSerialization(String preorder) {
if(preorder.equals("#"))
return true; String[] ss=preorder.split(",");
if(ss[0].equals("#"))
return false;
int b=2;
for(int i=1;i<ss.length;i++)
{
if(b<=0)
return false;
if(ss[i].equals("#"))
{b=b-1;}
else b=b+1; }
if(b!=0)
return false;
return true;
}
}

代码如下:(方法二:利用一个根节点有两个子节点)

 public class Solution {
public boolean isValidSerialization(String preorder) {
if(preorder.length()==0||preorder=="") return false;
String[] ss=preorder.split(",");
Stack<String> stack=new Stack<>(); for(int i=0;i<ss.length;)
{
while(stack.size()>=2&&stack.peek().equals("#")&&ss[i].equals("#"))
{stack.pop();stack.pop();}
if(stack.size()==1&&stack.peek().equals("#")&&i<ss.length-1)
return false;
else stack.push(ss[i++]);
} if(stack.size()==1&&stack.peek().equals("#"))
return true; return false;
}
}

331. Verify Preorder Serialization of a Binary Tree的更多相关文章

  1. leetcode 331. Verify Preorder Serialization of a Binary Tree

    传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...

  2. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  3. LeetCode OJ 331. Verify Preorder Serialization of a Binary Tree

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  4. 331. Verify Preorder Serialization of a Binary Tree -- 判断是否为合法的先序序列

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  5. 【leetcode】331. Verify Preorder Serialization of a Binary Tree

    题目如下: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...

  6. 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化

    序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #.     _9_    /   \   3     2  ...

  7. LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27

    331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...

  8. 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

  9. [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化

    One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...

随机推荐

  1. bzoj 1834: [ZJOI2010]network 网络扩容

    #include<cstdio> #include<iostream> #include<cstring> #define M 100000 #define inf ...

  2. 二模 (13)day1

    第一题: 题目大意: N个发射站排成一排,求每个发射站左右第一个比它高的发射站. N<=1000000 解题过程: 1.前几天做poj的时候刚好在discuss里看到有一个神奇的东东叫单调栈,正 ...

  3. 数列F[19] + F[13]的值

    已知数列如下:F[1]=1, F[2]=1, F[3]=5,......,F[n] =F[n-1] + 2*F[n-2],求F[19] + F[13]? #include <stdio.h> ...

  4. javaweb之Cookie篇

    Cookie是在浏览器访问某个Web资源时,由Web服务器在Http响应消息头中通过Set-Cookie字段发送给浏览器的一组数据. 一个Cookie只能表示一个信息对,这个信息对有一个信息名(Nam ...

  5. RPI学习--WebCam_mplayer

    1,安装mplayer $ sudo apt-get install mplayer 2,运行 $ sudo mplayer tv:// 有时会秀逗,绿屏,多试几下就好了,情况未知

  6. 指针属性直接赋值 最好先retain 否则内存释放导致crash

    //先释放之前的 YK_RELEASE_SAFELY(_selectedDate); //retain新的 _selectedDate = [aDate retain]; NSString 属性的好像 ...

  7. php中的include()的使用技巧

    php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...

  8. Java基础毕向东day02

    1. 常量 null 等特殊 2.标识符 数字-字母-下划线,数字不能开头 3.二进制 1> 二进制计算方法. 2>常用二进制. 1        1      0       0   1 ...

  9. NCBI原始数据下载by Aspera Connect

    主要参考这篇文章: http://mp.weixin.qq.com/s?__biz=MzA5NjU5NjQ4MA==&mid=2651154488&idx=1&sn=e693a ...

  10. 走进科学之WAF(Web Appllication Firewall)篇

    小编P.S:文章非常详尽对WAF领域进行了一次科普,能有让人快速了解当前WAF领域的相关背景及现状,推荐所有WAF领域的同学阅读本文. 1. 前言 当WEB应用越来越为丰富的同时,WEB 服务器以其强 ...