LeetCode-331 Verify Preorder Serialization of a Binary Tree

Title Description

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

 

Subject to the effect

To a string, the string is determined whether the sequence of binary tree after a previous sequence through.

(Tree node is represented by an integer, an empty node with '#' denotes, as delimiter ',')

 

Examples

E1

Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true

E2

Input: "1,#"
Output: false

E3

Input: "9,#,#,1"
Output: false

 

Problem-solving ideas

Nodes with a stack to store available, simply save the stack bool type, each encounter two '#' type will stack the stack while the stack becomes true of false, when the stack is false, representative of the left node to the stack, it is the stack until the stack is true, and to false.

 

Complexity Analysis

Time complexity: O (N)

Space complexity: O (N)

 

Code

class Solution {
 public :
     BOOL isValidSerialization ( String preorder) { 
        Stack < BOOL > Node;
         IF (preorder == " # " )
             return  to true ; 
        
        int I;
         // Traversal string 
        for (I = 0 ; I <preorder.length ( ); ++ I) {
             IF (preorder [I] == ' , ' )
                 Continue ;
             // if access to an empty node 
            else IF (preorder [I] == ' # ' ) {
                 // If the stack is empty, then the tree illegal exit loop 
                IF (node.empty ())
                     BREAK ;
                 // if the stack is true, it is to false 
                IF (node.top ()) { 
                    node.pop (); 
                    node.push ( false ); 
                } 
                // otherwise need to stack all the stack false 
                the else { 
                    node.pop (); 
                    IF (node.empty ())
                         BREAK ;
                     the while (node.empty (!) &&! node.top ()) {
                        node.pop();
                    }
                    if(!node.empty()) {
                        node.pop();
                        node.push(false);
                    }
                    if(node.empty())
                        break;
                }
            }
            // 否则代表访问到结点值
            else {
                int j = i + 1;
                while(j < preorder.length() && preorder[j] != '#' && preorder[j] != ',')
                    ++ J; 
                node.push ( to true ); 
                i = J - 1 ; 
            } 
        } 
        // If the early exit loop or stack is not empty, on behalf of illegal tree 
        IF (i = preorder.length () -! 1 ||! The Node .empty ())
             return  to false ;
         return  to true ; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/heyn1/p/11199995.html