Middle-题目51:331. Verify Preorder Serialization of a Binary Tree

原创 2016年05月31日 16:22:08

Middle-题目51: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, 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.
题目大意:
给出一个用逗号分隔的字符串,其中数字代表节点的值,#号代表空节点,判断是否是一个合法的二叉树的前序遍历序列。
题目分析:
方法一(朴素解法):用堆栈,每次遇到一个数字或者#号都入栈,如果栈顶的前三个元素依次是#,#,数字,则把它们三个都弹出来,再把#号压入栈(代表去掉了一个叶子节点),如此往复若最后栈里只有一个#(所有节点遍历完毕),则是合法的。
方法二:从discuss中看到的一个神算法,好像机理是一样的,只是没有用到堆栈,大致意思是,初始化一个变量count从后向前搜索,遇到#号就+1,遇到数字就-1,看最后是不是1,如果中途不够减则返回false。此算法具体思路及验证正确性,还有待进一步研究。
源码:(language:java)
方法一:

public class Solution {
    public boolean isValidSerialization(String preorder) {
        String[] nodes = preorder.split(",");
        Stack<String> stack = new Stack<String>();
        for(String str : nodes) {
            stack.push(str);
            int size = stack.size();
            while(size > 2 && stack.get(size-1).equals("#") && stack.get(size-2).equals("#") && !stack.get(size-3).equals("#")) {
                stack.pop();
                stack.pop();
                stack.pop();
                stack.push("#");
                size = stack.size();
            }   
        }
        return stack.size() == 1 && stack.peek().equals("#");
    }
}

方法二:

public class Solution {
    public boolean isValidSerialization(String preorder) {
        int len = preorder.length();
        int count = 0;
        for(int i=len-1; i>=0; i--){
            char tmp = preorder.charAt(i);
            if(tmp == ','){
                continue;
            }else if(tmp == '#'){
                count++;
            }else if(tmp != ',' && tmp != '#' && i!=0 && preorder.charAt(i-1)!=','){
                continue;
            }else{
                if(count<2){
                    return false;
                }else{
                    count--;
                }
            }
        }
        return count==1;
    }
}

成绩:
方法一:25ms,beats 10.82%,众数11ms,22.68%
方法二:4ms,beats 98.75%
Cmershen的碎碎念:
其实方法一浪费时间主要是在基于正则表达式的split函数和STL中堆栈函数,而方法二直接对数组操作,但似乎本质是一样的(因为方法二中的count也是统计的#号个数)。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

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

331. Verify Preorder Serialization of a Binary Tree(C++实现)

算法思想一:(二叉树中所有节点的出度=入度) 根结点有2出度0入度,分支结点有2出度1入度,空结点有0出度1入度。遍历序列,判断出度是否等于入度! 记 K=出度-入度。如果序列化是正确的,那么 K...

LeetCode 331. Verify Preorder Serialization of a Binary Tree(校验二叉树的前序遍历序列化)

原题网址:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ One way to seri...

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

leetcode 331. Verify Preorder Serialization of a Binary Tree

331. Verify Preorder Serialization of a Binary Tree题目大意给你一个树的前序遍历,其中空节点用#表示,在不重新构建这科树的前提下,判断序列是否可以构成...

[leetcode] 331. Verify Preorder Serialization of a Binary Tree 解题报告

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

331. Verify Preorder Serialization of a Binary Tree

Task: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-n...

190. Reverse Bits\331. Verify Preorder Serialization of a Binary Tree

Reverse Bits description implementation Verify Preorder Serialization of a Binary Tree description i...

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 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)