[Offer] [tree] to prove safety after the order binary search tree traversal sequence

Title: Enter an integer array, the array is determined not after a binary search tree in preorder traversal results. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

 

A: A postorder traversal of the binary tree, the root node is the last element of the array, the left subtree is always less than the root sequence, the sequence is always greater than the right subtree root

  Find and sequence the left subtree right subtree sequence

  Recursive call to find, if not meet the condition returns false, if the last left greater than right returns true

 

{Solution class 
public: 
    BOOL isBST (Vector <int> Sequence, int left, int right) 
    { 
        IF (left> = right) // == right corresponds to the left leaf node, left> right corresponding to an empty tree 
        { 
            return to true ; 
        } 
        int check_right = right; 
        // find the right subtree, tmp is a right subtree leftmost boundary point 
        the while ((check_right> left) && (Sequence [check_right -. 1]> Sequence [right])) 
        { 
            --check_right; 
        } 
        // Get the left subtree 
        for (int check_left check_right = -. 1; check_left> = left; --check_left) 
        { 
            IF (Sequence [check_left]> Sequence [right]) 
            { 
                return to false; 
            }
        }
        return (isBST(sequence, left, check_right - 1) && isBST(sequence, check_right, right - 1));
    }
    bool VerifySquenceOfBST(vector<int> sequence) {
        //{5,7,6,9,11,10,8}
        if(sequence.empty())
        {
            return false;
        }
        return isBST(sequence, 0, sequence.size() - 1);
    }
};

  

 

 Related topics:

  Calculator:

Enter an arithmetic expression as a string. It outputs the result after the calculation. If the string does not meet the arithmetic expression of the output string Error.
note:
Enter the number 0. The only non-negative integer. Decimals, negative numbers etc required output Error.
1. The need to support operators add, subtract, multiply, and parentheses.
2. The priority of operators: bracket> plus minus => multiplication.
3. any space between supports and operands, operators, and operator.
3. Enter the calculation process and no need to consider an integer overflow, to a 32-bit int. https://www.nowcoder.com/practice/fb2392f481d84a4cabb7cab9335a7896

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11432867.html