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. AngularJS recursive(递归)

    工作中我们经常要遍历多层数据,如果数据是已知层级的话,用 ng-repeat 就搞定了,要是数据深度是无限的呢,或者我们要实现一个无限层级的 tree 的时候,该怎么办? 答案是使用 ng-inclu ...

  2. JAVA与指针

    首先,提个问题:JAVA中没有指针,JAVA中有指针,哪个一个对呢? 答:都对,JAVA中没有指针,因为我们不能对指针直接操作,像C++那样用->来访问变量. JAVA有指针,因为JDK中封装了 ...

  3. 哈希(Hask)

     编辑 Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射 ...

  4. HDU 1358 简单kmp

    题目大意: 找到所有的可组成连续字符串相连的位置,和循环字符串的个数 #include <cstdio> #include <cstring> #include <alg ...

  5. IOS开发中--点击imageView上的Button没有任何反应

    点击imageView上的Button没有任何反应:    解决方法:设置图片的userInteractionEnabled为YES,使该imageView可以与用户进行交互

  6. JS原生回到顶部效果

    // 回到顶部 onload = function () { var oBtnTop = document.getElementById('toTop'); var timer = null; oBt ...

  7. jQuery easyui 提示框

    1:弹出提示窗的使用 (1)屏幕右下弹出提示窗口: $.messager.show({ title:'My Title', msg:'Message will be closed after 4 se ...

  8. oracle字符集的查看和修改

    Oracle修改字符集2.3oracle数据库的字符集更改 A.oracle server 端 字符集查询 select userenv(‘language’) from dual 其中NLS_CHA ...

  9. hdu 2091

    PS:PE了两次....又是这种奇怪的输出格式....两个三角形直接有空行.. 代码: #include "stdio.h" void ou(int n,char a); void ...

  10. 一道面试题:按照其描述要求用java语言实现快速排序

    回来想了想,写出了如下的程序: /** * 一道面试题,按照其描述要求进行快速排序(英文的,希望理解是对的..) * 要求:和一般的快速排序算法不同的是,它不是依次交换pivot和左右元素节点(交换2 ...