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
分析:https://www.hrwhisper.me/leetcode-verify-preorder-serialization-of-a-binary-tree/
这个方法简单的说就是不断的砍掉叶子节点。最后看看能不能全部砍掉。
以例子一为例,:”9,3,4,#,#,1,#,#,2,#,6,#,#” 遇到x # #的时候,就把它变为 #
我模拟一遍过程:
- 9,3,4,#,# => 9,3,# 继续读
- 9,3,#,1,#,# => 9,3,#,# => 9,# 继续读
- 9,#2,#,6,#,# => 9,#,2,#,# => 9,#,# => #
public boolean isValidSerialization(String preorder) {
Stack<String> stack = new Stack<String>();
String[] arr = preorder.split(","); for (int i = ; i < arr.length; i++) {
stack.push(arr[i]);
while (stack.size() >= && stack.get(stack.size() - ).equals("#")
&& stack.get(stack.size() - ).equals("#") && !stack.get(stack.size() - ).equals("#")) {
stack.remove(stack.size() - );
stack.remove(stack.size() - );
}
} if (stack.size() == && stack.peek().equals("#"))
return true; return false;
}
另一种解法:https://www.hrwhisper.me/leetcode-verify-preorder-serialization-of-a-binary-tree/
看了 dietpepsi 的代码,确实思路比我上面的更胜一筹:
In a binary tree, if we consider null as leaves, then
- all non-null node provides 2 outdegree and 1 indegree (2 children and 1 parent), except root
- all null node provides 0 outdegree and 1 indegree (0 child and 1 parent).
Suppose we try to build this tree. During building, we record the difference between out degree and in degree
diff
=outdegree - indegree
. When the next node comes, we then decreasediff
by 1, because the node provides an in degree. If the node is notnull
, we increase diff by2
, because it provides two out degrees. If a serialization is correct, diff should never be negative and diff will be zero when finished.from :https://leetcode.com/discuss/83824/7-lines-easy-java-solution
我这里翻译一下:
对于二叉树,我们把空的地方也作为叶子节点(如题目中的#),那么有
- 所有的非空节点提供2个出度和1个入度(根除外)
- 所有的空节点但提供0个出度和1个入度
我们在遍历的时候,计算diff = outdegree – indegree. 当一个节点出现的时候,diff – 1,因为它提供一个入度;当节点不是#的时候,diff+2(提供两个出度) 如果序列式合法的,那么遍历过程中diff >=0 且最后结果为0.
public boolean isValidSerialization(String preorder) {
String[] nodes = preorder.split(",");
int diff = ;
for (String node: nodes) {
if (--diff < ) return false;
if (!node.equals("#")) diff += ;
}
return diff == ;
}
Verify Preorder Serialization of a Binary Tree的更多相关文章
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- [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, ...
- 【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 Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题目: One way to ...
- 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, ...
- 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, ...
- [Swift]LeetCode331. 验证二叉树的前序序列化 | 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, ...
随机推荐
- JS所谓的享元模式-->
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- 基于Bootstrap的jQuery开关按钮插件
按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...
- C#给文件夹添加权限
//==== //添加权限 private void SetAttributes(string folder) { if (folder == "" || !Directory.E ...
- 【UVA 1451】Average
题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...
- 33.Android之Fragment学习
Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...
- 直接运行可执行文件linux终端一闪而过
运行elasticsearch的时候进入bin目录,ela 然后tab提示的内容中没有e..s..,很奇怪,然后我直接双击运行es,终端一闪而过,我就手动打开终端, ./elasticsearch 这 ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- sprintf
功能:将数据格式化到字符串中 原型:int sprintf( char *buffer, const char *format, [ argument] … );返回值是这个字符串的长度 上次我企图这 ...
- js校验表单后提交表单的三种方法总结
第一种: 复制代码 代码如下: <script type="text/javascript"> function check(form) { if(fo ...
- 初学structs2,表单验证
一.简单表单验证示例 structs.xml配置 <struts> <package name="validate" namespace="/valid ...