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, 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的更多相关文章
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【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 ...
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- 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, ...
- 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, ...
- 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化
序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #. _9_ / \ 3 2 ...
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...
- 【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 ...
- [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, ...
随机推荐
- Java学习总结(二)----Java语言基础
1. Java语言基础 2.1 关键字 定义:被java语言赋予特殊含义的单词 特点:关键字中的字母都为小写 用于定义数据类型的关键字 class,interface,byte,short,i ...
- C#后台程序重启IIS,发邮件通知
应用场景:IIS网站挂掉,系统自动重启IIS,通知相关联系人: 主要代码: 监控类 public class monitoringiis { EmailSend send = new EmailSen ...
- LeetCode "Valid Perfect Square"
Typical binary search.. but take care of data overflow if you are using C++ class Solution { public: ...
- 基于adt-bundle-windows-x86的android开发环境搭建
0,简介: 最近简单着手了解 android 开发.工欲善其事,必先利其器. 我本人不太喜欢使用java 开发,所以简单了解了下其 c# c++都可以进行android 开发,用c++的话要使用NDK ...
- 对ASP.NET运行机制之 一般处理程序ashx的学习
一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名.其中一个httpHandler接受并处理一个http请求,类比于Java中的servlet.类比于在Java中 ...
- 随便2--struct pointer
同为struct,如果struct中没有指针, C 和C++可以用等号赋值,但是一旦里面涉及到指针,就不能用等号,要用memcpy struct A{char v1[20];int v2;} a,b; ...
- 磁盘IO的性能指标
磁盘IO的性能单位: bps (byte/s) 每秒钟 byte 数 kbps = bps / 1024 mbps = kbps / 1024 相关计算可以参考下面代码: https://git ...
- Oracle linux6.1配置yum本地源
一.准备工作1. Linux安装盘插入光驱2. 挂载光驱 [root@localhost ~]# mount /dev/cdrom /mnt/ mount: block device /dev/sr0 ...
- (String)151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 【原】升级nginx注意点
1.通知nginx将pid文件改名为nginx.pid.oldbin,并启动新的nginx kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` 2.通知旧 ...