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

331. Verify Preorder Serialization of a Binary Tree

时间:2016-07-10 07:37:31      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 331. Verify Preorder Serialization of a Binary Tree
     * 2016-7-9 by Mingyang
     * 这个题目我的绝对原创的思路是把number##变为#,这样不断地俄罗斯方块式的减少
     * 到最后只剩下一个#,所以自己就这么做,然后用StringBuffer的replace功能
     * 但是发现有一个case过不去,就是"9,#,92,#,#"为什么呢?就是因为我只把单个digit
     * 的数考虑进去,没有看92作为一个整体。
     * 然后参考网上的stack的做法,一样的思路
     * 另外还有非常巧妙的大神,用出度跟入度一样来做,真的是非常棒!
     */
    //自己最初的做法
    public static boolean isValidSerialization(String preorder) {
        int len = preorder.length();
        if (preorder == null || len == 0)
            return true;
        preorder = preorder.replaceAll(",", "");
        StringBuffer sb = new StringBuffer(preorder);
        int i = sb.length();
        while (i >= 3) {
            while (i > 2) {
                if (sb.charAt(i - 1) == ‘#‘ && sb.charAt(i - 2) == ‘#‘ && sb.charAt(i - 3) != ‘#‘) {
                    sb.replace(i - 3, i, "#");
                    i = sb.length();
                } else {
                    i--;
                }
            }
            break;
        }
        if (sb.toString().equals("#"))
            return true;
        return false;
    }
     //Stack的做法:
    public boolean isValidSerialization1(String preorder) {
        // using a stack, scan left to right
        // case 1: we see a number, just push it to the stack
        // case 2: we see #, check if the top of stack is also #
        // if so, pop #, pop the number in a while loop, until top of stack is
        // not #
        // if not, push it to stack
        // in the end, check if stack size is 1, and stack top is #
        if (preorder == null) {
            return false;
        }
        Stack<String> st = new Stack<>();
        String[] strs = preorder.split(",");
        for (int pos = 0; pos < strs.length; pos++) {
            String curr = strs[pos];
            while (curr.equals("#") && !st.isEmpty() && st.peek().equals(curr)) {
                st.pop();
                if (st.isEmpty()) {
                    return false;
                }
                st.pop();
            }
            st.push(curr);
        }
        return st.size() == 1 && st.peek().equals("#");
    }
    //出度,入度的做法:
    public boolean isValidSerialization2(String preorder) {
        String[] strs = preorder.split(",");
        int degree = -1; // root has no indegree, for compensate init with -1
        for (String str : strs) {
            degree++; // all nodes have 1 indegree (root compensated)
            if (degree > 0) { // total degree should never exceeds 0
                return false;
            }
            if (!str.equals("#")) {// only non-leaf node has 2 outdegree
                degree -= 2;
            }
        }
        return degree == 0;
    }

 

331. Verify Preorder Serialization of a Binary Tree

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5657051.html

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