zoukankan      html  css  js  c++  java
  • 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. 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 Credits: Special thanks to @dietpepsi for adding this problem and creating all test cases.

    找规律?

    我们通过举一些正确的例子,比如"9,3,4,#,#,1,#,#,2,#,6,#,#" 或者"9,3,4,#,#,1,#,#,2,#,6,#,#"等等,可以观察出如下两个规律:

    1. 数字的个数总是比#号少一个

    2. 最后一个一定是#号

    那么我们加入先不考虑最后一个#号,那么此时数字和#号的个数应该相同,如果我们初始化一个为0的计数器,遇到数字,计数器加1,遇到#号,计数器减1,那么到最后计数器应该还是0。下面我们再来看两个返回False的例子,"#,7,6,9,#,#,#"和"7,2,#,2,#,#,#,6,#",那么通过这两个反例我们可以看出,如果根节点为空的话,后面不能再有节点,而且不能有三个连续的#号出现。所以我们再加减计数器的时候,如果遇到#号,且此时计数器已经为0了,再减就成负数了,就直接返回False了,因为正确的序列里,任何一个位置i,在[0, i]范围内的#号数都不大于数字的个数的。当循环完成后,我们检测计数器是否为0的同时还要看看最后一个字符是不是#号。

    public class Solution {
        public boolean isValidSerialization(String preorder) {
            if (preorder == null || preorder.length() == 0) return false;
            String[] strs = preorder.split(",");
            int depth = 0;
            int i = 0; 
            while (i < strs.length - 1) {
                if (strs[i++].equals("#")) {
                    if (depth == 0) return false;
                    else depth--;
                }
                else depth++;
            }
            if (depth != 0) return false;
            return strs[strs.length - 1].equals("#");
        }
    }
    

      

    出度入度

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

      

  • 相关阅读:
    Android在layout xml中使用include完成静态加载
    ImageSwitch图像切换控件
    合并石子大总结
    子矩阵(暴搜(全排列)+DP)
    回路(一笔画问题)
    道路重建(记忆化搜索+贪心)
    【NOIP2013 普及组】车站分级
    UML的基本关联
    Matlab画图-非常具体,非常全面
    面向对象程序设计与面向过程程序设计解析
  • 原文地址:https://www.cnblogs.com/apanda009/p/7300706.html
Copyright © 2011-2022 走看看