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

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

解答

这题好坑啊,逗号也要读取。用到的方法是如果是先序遍历的结果的话遍历结束后的数组NULL节点数一定比非空节点恰好多1。而这里要注意的是非空节点的键值可能在字符串数组中是多位数,并且如果字符串最后以数结尾的话,要注意此时是没有逗号的,所以不能简单的以判断逗号作为一个多位数结束的标志。

bool isValidSerialization(char* preorder) {
    , ch = , i, j;
    ; preorder[i] != ; i = i + ){
        if('#' != preorder[i]){
            num++;
            ; j++);
            )
                break;
            i = j - ;
        }
        else{
            ch++;
        }
         == ch){
            break;
        }
    }
    ] == &&num +  == ch){
        return true;
    }
    else{
        return false;
    }
}

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

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

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

  2. 【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 ...

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

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

  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. 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, ...

  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] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack

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

随机推荐

  1. (转)Deep Learning Research Review Week 1: Generative Adversarial Nets

    Adit Deshpande CS Undergrad at UCLA ('19) Blog About Resume Deep Learning Research Review Week 1: Ge ...

  2. 1、C#入门第一课

    C# 读作C Sharp,所以程序文件的扩展名为.cs 新建项目-窗体应用程序 所谓的Visual C#就是指的可视化编程,主要在设计窗口布置好自己的控件(一些具有一定功能的小部件,例如如可以点击的按 ...

  3. CentOS 7 httpd服务的安装、配置

    关于linux   httpd的搭建和配置都需要基于yum源 1:安装httpd yum linstall httpd -y 2:进入html文件 cd /var/www/html 讲已经编辑的htm ...

  4. elasticsearch单机多实例环境部署

    elasticsearch的功能,主要用在搜索领域,这里,我来研究这个,也是项目需要,为公司开发了一款CMS系统,网站上的搜索栏功能,我打算采用elasticsearch来实现. elasticsea ...

  5. redis集群的一些笔记

    当节点数量少于6个时候会提示如下信息,初始化一个集群的时候需要6个节点,为什么?? *** ERROR: Invalid configuration for cluster creation. *** ...

  6. Unity5版本的AssetBundle打包方案之打包Scene场景

    using UnityEngine; using System.Collections; using UnityEditor; /// <summary> /// 脚本位置:Editor文 ...

  7. Django进阶篇(一)

    Form django中的Form一般有两种功能: 1.输入html 2.验证用户输入 最简易的form验证: <!DOCTYPE html> <html lang="en ...

  8. 数据库 MySql

    MySql 一,概述 1,什么是数据库? 数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 二,下载安装 想要使用MySQL来存储并操作数据,则需要做几件事情: a. 安装My ...

  9. Struts2.3.4+Hibernate4.2.4+Mysql6.0整合

    1.项目搭建过程: (1). 创建一个Web Project.导入Struts2和Hibernate的jar包.(如果不知道Struts2的jar包,可以在下载的struts的jar包中,找到apps ...

  10. C预处理和C库

    #include <stdio.h> #define MAN(x) "n"##x int main(void) { printf("%s",MAN( ...