序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时,我们可以记录这个节点的值。如果它是一个空节点,我们可以使用一个标记值,例如 #。
     _9_
    /   \
   3     2
  / \   / \
 4   1  #  6
/ \ / \   / \
# # # #   # #
例如,上面的二叉树可以被序列化为字符串"9,3,4,#,#,1,#,#,2,#,6,#,#",其中#代表一个空节点。
给定一串以逗号分隔的值,验证它是否是正确的二叉树的前序序列化。想出一种在不重构树的情况下的可行算法。
每个逗号分隔的字符串中的值肯定是一个整数或者一个表示null指针的'#'。
你可以假定输入格式总是有效的,例如它永远不能包含两个连续的逗号,比如"1,,3"。
示例 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
返回 true
示例 2:
"1,#"
返回 false
示例 3:
"9,#,#,1"
返回 false

详见:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/description/

C++:

class Solution {
public:
bool isValidSerialization(string preorder) {
istringstream in(preorder);
vector<string> v;
string t = "";
int cnt = 0;
while (getline(in, t, ','))
{
v.push_back(t);
}
for (int i = 0; i < v.size() - 1; ++i)
{
if (v[i] == "#")
{
if (cnt == 0)
{
return false;
}
--cnt;
}
else
{
++cnt;
}
}
return cnt == 0 && v.back() == "#";
}
};

参考:https://www.cnblogs.com/grandyang/p/5174738.html

331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化的更多相关文章

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

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

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

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

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

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

  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 -- 判断是否为合法的先序序列

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

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

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

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

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

随机推荐

  1. 洛谷 4364 [九省联考2018]IIIDX

    [题解] 一眼可以想到一个类似二叉树后序遍历的贪心做法,然而这个做法在有相同数字的情况下是错误的.最简单的反例就是n=4,d={1,1,1,2},正解是1,1,2,1,而贪心是1,1,1,2. 所以这 ...

  2. CSS3制作404立体字体

    CSS3制作404立体字体页面效果     鼠标移动上去,背景色变白.       动态效果:     .demo p:first-child span:hover { text-shadow:0px ...

  3. 模拟select控件功能

    直接上代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  4. 46. Spring Boot中使用AOP统一处理Web请求日志

    在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...

  5. Win32编程API 基础篇 -- 2.一个简单的窗口 根据英文教程翻译

    一个简单的窗口 例子:简单的窗口 有时人们在IRC提问,”我应该怎样制作一个窗口”...嗯,这恐怕不是完全这么简单好回答!其实这并不难一旦你明白你在做什么,但在你得到一个可展示的窗口之前还有一些事情需 ...

  6. WinForm 中限制只能输入数字

    在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值.限制只能输入数字,小数点,Backspace,del这几个键.数字0~9所对应的 ...

  7. P3372 【模板】线段树 1 洛谷

    https://www.luogu.org/problem/show?pid=3372 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 ...

  8. yum 源本地化 (two)

    之前写过一个yum源本地化的文章. 后来发现那个方法有些缺陷, yum install --downloadonly --downloaddir=/tmp/atomicdownload memcach ...

  9. angularjs 过滤多组数据

    <html> <head> <script src="angular.min.js"></script> <script ty ...

  10. uboot arp地址解析

    common/cmd_cache.c int do_getmac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) {     //usend d ...