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 # #的时候,就把它变为 #

我模拟一遍过程:

  1. 9,3,4,#,# => 9,3,# 继续读
  2. 9,3,#,1,#,# => 9,3,#,# => 9,# 继续读
  3. 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 decrease diff by 1, because the node provides an in degree. If the node is not null, 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 == ;
}

最新文章

  1. SQL 创建随机时间的函数
  2. Android 使用xml序列化器生成xml文件
  3. mvc上传到云虚拟机的问题解决
  4. [LeetCode] Longest Consecutive Sequence
  5. 如何用ZBrush快速雕刻LOL中的Lissandra
  6. 如果你喜欢Python 那么你不得不知的几个开源项目
  7. Microsoft.DirectX.DirectSound学习(一)
  8. 微软职位内部推荐-Senior Engineering Lead
  9. 网站运维工具使用iis日志分析工具分析iis日志(iis日志的配置)
  10. Spring+SpringMVC+MyBatis+easyUI整合优化篇(五)结合MockMvc进行服务端的单元测试
  11. Lua的线程和状态
  12. python之进程,线程,协程简单理解
  13. 深入Java集合学习系列:LinkedHashMap的实现原理
  14. asp.net core发布到linux
  15. Appium-desktop安装启用Inspector一直报错An unknown server-side error occurred...
  16. 从零开始unity特效(持续追加中)
  17. canvas-star5.html
  18. Windows 系统安装多个版本JDK, 修改环境变量不生效
  19. Dataguard 主库与备库的Service_Name 不一致时,如何配置客户端TNSName
  20. Spark 数据源

热门文章

  1. OC基础--分类(category) 和 协议(protocol)
  2. Luncene 学习入门
  3. WAR包
  4. oracle基本语句
  5. 【bzoj1036】 ZJOI2008—树的统计Count
  6. CodeForces 559C Gerald and Giant Chess
  7. php中图片文件的导入,上传与下载
  8. nginx+php出现502 不能解析
  9. linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg)
  10. 锋利的jQuery-4--图片切换的一个例子(自己理解后写的,以备忘记时看看)