资讯专栏INFORMATION COLUMN

Verify Preorder Serialization of a Binary Tree

melody_lql / 1148人阅读

摘要:令,那么从累加会一直保持正,最后为。从左往右比较好理解,因为总是总到左再到右,的总是的,所以一定是保持。注意从开始,因为没有。

Verify Preorder Serialization of a Binary Tree

题目链接:https://leetcode.com/problems...

recursion,用个全局的index:

public class Solution {
    public boolean isValidSerialization(String preorder) {
        if(preorder == null || preorder.length() == 0) return false;
        String[] tree = preorder.split(",");
        if(tree.length == 1) return tree[0].equals("#");
        
        return dfs(tree) && tree.length == index;
    }
    int index = 0;
    private boolean dfs(String[] tree) {
        // start from root
        if(index >= tree.length) return false;
        if(tree[index].equals("#")) {
            index++;
            return true;
        }
        index++;
        return dfs(tree) && dfs(tree);
    }
}

iteration:

public class Solution {
    public boolean isValidSerialization(String preorder) {
        if(preorder == null || preorder.length() == 0) return false;
        // iteration, add the node
        Stack stack = new Stack();
        for(String s : preorder.split(",")) {
            // check 2 "#"
            if(s.equals("#")) {
                while(!stack.isEmpty() && stack.peek().equals("#")) {
                    // pop "#"
                    stack.pop();
                    if(stack.isEmpty()) return false;
                    // pop parent of "#" & "#"
                    stack.pop();
                }
            }
            stack.push(s);
        }
        
        return stack.size() == 1 && stack.peek().equals("#");
    }
}

看到discussion还有一种用入度出度的方法,比较厉害,不用额外空间:
除了根节点和叶子节点之外,每个节点都有2个出度(left,right)和1个入度,根节点非空有2个出度0个入度,叶子节点有1个入度。令diff = outdegree - indegree,那么从累加diff会一直保持正,最后为0。从左往右比较好理解,因为preorder总是总root到左再到右,root的diff总是>0的,所以一定是保持positive。从右往左就不知道怎么理解了。。注意diff从1开始,因为root没有indegree。

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66598.html

相关文章

  • leetcode331. Verify Preorder Serialization of a Bi

    摘要:如果我们从右往左看先序遍历,就知道后两个节点如果遇到第三个节点,则该节点就应当是这两个节点的父节点。如果在遍历的过程中根节点数量小于,则说明这棵树有问题。而如果遍历结束之后,剩下的根节点数不等于,也说明这个先序遍历存在问题。 题目要求 One way to serialize a binary tree is to use pre-order traversal. When we en...

    weapon 评论0 收藏0
  • LeetCode 331. Verify Preorder Serialization of a B

    摘要:如果它是一个空节点,我们可以使用一个标记值记录,例如。例如,上面的二叉树可以被序列化为字符串,其中代表一个空节点。每个以逗号分隔的字符或为一个整数或为一个表示指针的。如果满足前序遍历,那么最后栈中有且仅有一个元素,且是。 Description One way to serialize a binary tree is to use pre-order traversal. When ...

    张巨伟 评论0 收藏0
  • Serialize and Deserialize Binary Tree &amp; BST

    摘要:思路理论上说所有遍历的方法都可以。但是为了使和的过程都尽量最简单,是不错的选择。用作为分隔符,来表示。复杂度代码思路这道题和之前不同,一般的树变成了,而且要求是。还是可以用,还是需要分隔符,但是就不需要保存了。 297. Serialize and Deserialize Binary Tree Serialization is the process of converting a...

    eccozhou 评论0 收藏0
  • [Leetcode] Verify Preorder Sequence in Binary Sear

    摘要:如果继续降序,说明又向左走了,这样等到下次向右走得时候也要再次更新最小值。这样,序列无效的条件就是违反了这个最小值的限定。我们同样可以用本题的方法解,不过是从数组的后面向前面遍历,因为在后面了。栈的增长方向也是从高向低了。 Verify Preorder Sequence in Binary Search Tree Given an array of numbers, verify ...

    未东兴 评论0 收藏0
  • leetcode449. Serialize and Deserialize BST

    摘要:题目要求将二叉搜索树序列化和反序列化,序列化是指将树用字符串的形式表示,反序列化是指将字符串形式的树还原成原来的样子。假如二叉搜索树的节点较多,该算法将会占用大量的额外空间。 题目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...

    Honwhy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<