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 decreasediffby 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, ...
随机推荐
- Ibatis中传List参数
Ibatis中用list传参数的方式. Java代码 select count(id) from `user` where id in #[]# and status=1 . <select ...
- HTML布局篇之双飞翼(圣杯)布局
最近在写页面的时候,总是为布局头疼,倒不是不能布出来,就是感觉不系统,没有成一个体系的感觉.所以决定自己写博文,梳理一下思路. 常用的布局方式大致可以分为三种: 浮动布局 Float 负边距(双飞翼) ...
- 用SQL打印乘法口诀表
--用SQL打印出乘法口诀表 declare @i int ,@j int --@i是乘法口诀的行数 --一共九行 begin --每次都是从1*开始,j每循环一次递增 )--print每次输出都会换 ...
- hdu4825 字典树 XOR
用字典树思想来做.对于一个数,给出他的二进制,然后更具二进制建立字典树,然后每次询问的时候的数也给出二进制,如果当前为1,那就向0走,为0,向1走. #include<stdio.h> # ...
- Java常见异常
1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException 数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...
- HTTP各个状态返回值
转载来自于:http://desert3.iteye.com/blog/1136548 502 Bad Gateway:tomcat没有启动起来 504 Gateway Time-out: nginx ...
- office 软件常用备忘(删除线/冻结等)
Word: 1 大纲显示/navigation display: 视图/View --> Navigation Pane (left) 2 Track changes: 显示删除线 / 修改地方 ...
- javaIO(二)
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...
- c链表实现遇到的错误
想完成一个链表发现有错误,代码如下: //http://ac.jobdu.com/problem.php?pid=1511 //֮ǰÓÃlistʵÏֵ쬽ñÌìÊÔÒ»ÏÂÓÃstruct ...
- 修改MYSQL 表中的字段属性
1.登录数据库 >mysql -u root -p 数据库名称 2.查询所有数据表 >show tables; 3.查询表的字段信息 >desc 表名称; 4.1.修改某个表的字段类 ...