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 == ;
}

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

  1. leetcode 331. Verify Preorder Serialization of a Binary Tree

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

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

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

  3. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  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(331)

    1. Description One way to serialize a binary tree is to use pre-order traversal. When we encounter a ...

  6. LeetCode Verify Preorder Serialization of a Binary Tree

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

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

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

随机推荐

  1. Vijos p1892 树上的最大匹配 树形DP+计数 被卡常我有特殊技巧heheda

    https://vijos.org/p/1892 此题需要手动开栈: <<; //256MB char *p=(char*)malloc(size)+size; __asm__(" ...

  2. Canvas识别相似图片

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 【POJ 2886】Who Gets the Most Candies?

    题意 约瑟夫问题的升级版,每次出去的是前一个出去的人位置+手上的数字(正往前,负往后).第i个出去的人拿的糖是i的约数的个数.求拿糖最多的人和他的糖果数. 分析 线段树单点更新,反素数. 我竟然WA在 ...

  4. Mac OS X系统下编译运行C代码

    1.使用编译器将源文件中的代码转换为二进制代码,这个过程叫做编译. 将终端的工作路径切换到源文件所在的路径. cc -c 源文件的名称.例如:cc -c main.c 如果没有意外的话,就会在当前工作 ...

  5. easyUI框架之学习1--框架

    <!DOCTYPE html><html> <head > <link href="~/Scripts/easyUI/themes/default/ ...

  6. std::auto_ptr

    auto_ptr是C++标准库中(<utility>)为了解决资源泄漏的问题提供的一个智能指针类模板(注意:这只是一种简单的智能指针) auto_ptr的实现原理其实就是RAII,在构造的 ...

  7. Struts2拦截器的应用

    拦截器类 public class AdminInterceptor extends AbstractInterceptor { private static final long serialVer ...

  8. (转)DoDataExchange执行时机

    void CRegisterDialog::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DAT ...

  9. .NET中的装饰器设计模式

  10. Jquery Validate 正则表达式实用验证代码

    jQuery.validate 的正则验证功能,包括手机号码.电话号码.邮政编码.QQ号码.IP地址.字母和数字.中文的验证等. 手机号码验证 以下为引用内容:  jQuery.validator.a ...