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. 数迹学——Asp.Net MVC4入门指南(4):添加一个模型

    一.添加模型类 二.添加MovieDBContext类,连接数据库 DbContext类继承自 System.Data.Entity; 负责在数据库中获取,存储,更新,处理实例 MovieDBCont ...

  2. MySQL物理文件组成

    日志文件 错误日志:Error Log 错误日志记录了MySQL运行过程中所有较为严重的警告和错误信息,以及MySQL Server每次启动和关闭的详细信息.在默认情况下,系统记录错误日志的功能是关闭 ...

  3. MSSQL 生成拼音码

    MSSQL 生成拼音码 /*============================================================================== 名称:fn_G ...

  4. 使用yield关键字来提高性能

    比如我们在开发当中往往会遇到这样的问题: public List<string> FindBobs(string [] names)   { List<string> bobs ...

  5. StackOverflow程序员推荐的几本书籍

    1. <代码大全>史蒂夫·迈克康奈尔 推荐数:1684 “优秀的编程实践的百科全书,<代码大全>注重个人技术,其中所有东西加起来,就是我们本能所说的“编写整洁的代码”.这本书有 ...

  6. NUnit笔记

    注意:单元测试中,Case 与 Case 之间不能有任何关系 测试方法不能有返回值,不能有参数,测试方法必须声明为 public [TestFixture] //声明测试类 [SetUp] //建立, ...

  7. Charles V4系列更新 | 绿色特别版 | 视频教程

    Charles V4.0 视频教程 http://www.cnblogs.com/weimjsam/p/5841816.html Charles V4系列 绿色特别版,解压密码:博客名 链接: htt ...

  8. angular.js input

    <!DOCTYPE html> <html ng-app="app"> <head> <meta charset="UTF-8& ...

  9. Python正则表达式指南

    1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...

  10. CRM Xrm.Page 的对象层次结构