[Leetcode] 331. Verify-preorder-serialization-of-a-binary-tree (stack) [medium]

link

https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree/

time consuming

Problem solving: 21 min
Problem solving: 27 min

Title

One way to serialize a binary tree is to use preorder traversal. When we encounter a non-empty node, we can record the value of this node. If it is an empty node, we can use a tag value record, such as #.

     _9_
    /   \
   3     2
  / \   / \
 4   1  #  6
/ \ / \   / \
# # # #   # #

For example, the above binary tree can be serialized into the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents an empty node.

Given a comma-separated sequence, verify that it is the correct preorder serialization of the binary tree. Write a feasible algorithm without reconstructing the tree.

Each comma-separated character is either an integer or a'#' representing a null pointer.

You can think that the input format is always valid, for example, it will never contain two consecutive commas, such as "1,3".

Ideas

The question can be understood as that the string contains only leaf nodes ('#') and non-leaf nodes (not'#'), and it is judged whether the string is the preorder sequence of a tree. The preorder sequence first visits the root node and then the left and right subtrees, so if the preorder sequence of the left and right subtrees is reasonable and the root node exists, the current tree is reasonable.

Using a stack, the basic idea is to eliminate the current node if the two subtrees of the current node are reasonable. Specifically, if a non-leaf node is encountered, it will be pushed onto the stack. If a leaf node is encountered, it will be checked whether the top of the stack is a leaf node. If it is a non-leaf node, then the previous element of the stack top element is reasonable. The tree becomes a leaf node, that is, they are popped from the stack and are ready to be pushed into a leaf node. This is equivalent to encountering a leaf node, so continue to check until the top element of the stack is no longer a leaf node, and then push a leaf into the stack node.

Finally, if there is only one leaf node left in the stack after the traversal is completed, the preorder sequence is reasonable.

Time complexity: O (n) O(n)O ( n )

AC code

class Solution {
    
    
public:
    bool isValidSerialization(string preorder) {
    
    
        stack<int> st;
        int n = preorder.size();
        for(int i = 0; i < n; ++i) {
    
    
            if(preorder[i] == ',') {
    
    
                continue;
            }
            else if(preorder[i] == '#') {
    
    
                while(!st.empty() && st.top() == '#') {
    
    
                    if(st.size() < 2) return false;
                    st.pop();
                    st.pop();
                }
                st.push('#');
            }
            else {
    
    
                while(preorder[i] >= '0' && preorder[i] <= '9') {
    
    
                    i++;
                }
                st.push('*');
            }
        }
        return st.size() == 1 && st.top() == '#';
    }
};

Guess you like

Origin blog.csdn.net/Krone_/article/details/114697553