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_
/ \ / \ / \
#
/ \ / \ / \
# # # # # #

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

思路:将字符串按照逗号分隔,然后依次读入节点。用一个栈来记录当前的节点是左孩子还是右孩子。

 class Solution {
public:
bool isValidSerialization(string preorder) {
istringstream iss(preorder);
string node;
stack<bool> leftPathTrack;
bool tracked = false;
while (getline(iss, node, ',')) {
if (tracked && leftPathTrack.empty()) return false;
tracked = true;
if (node == "#") {
//for the case the root node is '#'
if (leftPathTrack.empty()) continue;
/*
If the current node is left child, pop it and mark the next node as right.
Else, pop all the nodes in the stack until the top node is a left child,
pop it and mark the next node as right.
*/
while (!leftPathTrack.empty() && !leftPathTrack.top())
leftPathTrack.pop();
if (!leftPathTrack.empty()) {
leftPathTrack.pop();
leftPathTrack.push(false);
}
} else {
leftPathTrack.push(true);
}
}
return leftPathTrack.empty();
}
};

Verify Preorder Serialization of a Binary Tree -- LeetCode的更多相关文章

  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

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

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

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

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

  5. LeetCode Verify Preorder Serialization of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...

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

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

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

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

随机推荐

  1. 【POJ 1201 Intervals】

    Time Limit: 2000MSMeamory Limit: 65536K Total Submissions: 27949Accepted: 10764 Description You are ...

  2. Avito Cool Challenge 2018 A. B题解

    A. Definite Game 题目链接:https://codeforces.com/contest/1081/problem/A 题意: 给出一个数v,然后让你可以重复多次减去一个数d,满足v% ...

  3. POJ1459:Power Network(多源点多汇点的最大流)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 31086   Accepted: 15986 题 ...

  4. inflate

    LayoutInflater是用 来找res/layout/下的xml布局文件,并且实例化 https://www.cnblogs.com/savagemorgan/p/3865831.html

  5. 自旋锁、排队自旋锁、MCS锁、CLH锁

    转载自:http://coderbee.net/index.php/concurrent/20131115/577 自旋锁(Spin lock) 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他 ...

  6. eclipse console输出有长度限制

    抓取一个网页内容,然后打印到控制台,发现内容首部都没有了. String content = getResponseText("http://xxx.html"); System. ...

  7. expect 实现本地到远程的scp

    expect文件demo 令文件名为test.exp #!/usr/bin/expect -f set timeout -1 set pwd " set src_file [lindex $ ...

  8. 群联MPALL(Rel) 7F V5.03.0A-DL07量产工具 PS2251-07(PS2307)

    前言:U盘被写保护,真的很醉人啊~~      群联MPALL是一款群联PS2251系列主控量产修复工具,本版本支持PS2251-67.PS2251-68.PS2251-02.PS2251-03.PS ...

  9. 在Debian9安装node和npm

    这学期又快结束了,坐在每天面对的电脑面,本着整理资料.更换心情的目的,我重装了一下自己的debian.下面就将自己安装node的过程进行记录与分享. node的官网:https://nodejs.or ...

  10. iconfont字体图标

    1.1.进入阿里图标网站 http://www.iconfont.cn/ 1.2.在购物车里添加自己需要的字体图标 1.3.下载代码 1.4.解压过后,找到iconfont.css,放在你的项目里,需 ...