码迷,mamicode.com
首页 > 其他好文 > 详细

Verify Preorder Serialization of a Binary Tree

时间:2016-08-04 01:15:21      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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,#,# => #
 1 public boolean isValidSerialization(String preorder) {
 2         Stack<String> stack = new Stack<String>();
 3         String[] arr = preorder.split(",");
 4 
 5         for (int i = 0; i < arr.length; i++) {
 6             stack.push(arr[i]);
 7             while (stack.size() >= 3 && stack.get(stack.size() - 1).equals("#")
 8                     && stack.get(stack.size() - 2).equals("#") && !stack.get(stack.size() - 3).equals("#")) {
 9                 stack.remove(stack.size() - 2);
10                 stack.remove(stack.size() - 2);
11             }
12         }
13 
14         if (stack.size() == 1 && stack.peek().equals("#"))
15             return true;
16 
17         return false;
18     }

另一种解法: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.

1 public boolean isValidSerialization(String preorder) {
2     String[] nodes = preorder.split(",");
3     int diff = 1;
4     for (String node: nodes) {
5         if (--diff < 0) return false;
6         if (!node.equals("#")) diff += 2;
7     }
8     return diff == 0;
9 }

 

Verify Preorder Serialization of a Binary Tree

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5735077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!