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. 安装Adobe Dreamweaver CS6 免序列号 官方破解版

    Adobe Dreamweaver CS6 免序列号 官方破解版 Adobe Dreamweaver CS6是世界顶级软件厂商Adobe推出的一套可视化的网页开发工具,Dreamweaver CS6最 ...

  2. POJ 1436 区间染色

    Horizontally Visible Segments Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4507   Ac ...

  3. HDU 1394 Minimum Inversion Number

    //============================================================================ // Name : B.cpp // Au ...

  4. Mongoose全面理解

    一.创建schemas 创建schemas的方式: 1 var userSchema = new mongoose.Schema({ 2 name: String, 3 email: String, ...

  5. iOS解决两个静态库的冲突 duplicate symbol

    http://blog.163.com/023_dns/blog/static/118727366201391544630380/ 场景: 解决TencentOpenAPI.framework与Zba ...

  6. 倍增 LCA

    以NOIP2013提高组day1 最后一道题为例来学的倍增和lca.其实这套题早就做过了,倍增和lca也学过,只不过当时没有理解清楚,所以今天再次学了一遍,虽然没有时间编程序了,但是先把思路和做法在这 ...

  7. 支持单色条码图像生成的条形码控件Barcode Professional

    Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...

  8. 转:gartner 2014-07 ETL工具象限排名

    ref:  http://www.gartner.com/technology/reprints.do?id=1-1YAXV15&ct=140728&st=sb

  9. UIView 翻转动画

    [_mapView removeFromSuperview]; [self addSubview:_tableView]; //应将self.view设置为翻转对象 [UIView transitio ...

  10. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...