After the title sequence of 24 binary search tree traversal

////////////////////////////////////////////////// ///////////////////////////////////
// 6. Title 24 binary search tree postorder traversal
/ / enter an integer array, the array is determined after the sequence is not a binary search tree traversal results! ! !
/ *  
         8
        / \
       610
      / \ / \
     57911

    Input array: {5, 7, 6, 9, 11, 10, 8}

    Left // -> Right -> in
     postorder traversal: 576,911,108
features:
     1. The last element is a root node
     2 is smaller than the left subtree of the root node, the root node is greater than the right subtree

*/
//



BOOL BinaryTreePostTraval ( int aiArray [], int iLen) 
{ 
    IF (== NULL iLen aiArray || <= 0 ) 
    { 
        return  to false ; 
    } 

    int iRootVal = aiArray [iLen - . 1 ];
     int lLeft = 0 ; 

    // 1. Left smaller than the sub-tree root 
    for (; lLeft <iLen - . 1 ; lLeft ++ ) 
    { 
        IF (aiArray [lLeft]> iRootVal) 
        { 
            BREAK ; 
        } 
    } 

    // 2. right subtree of the root node than the large 
    int= IRight lLeft;
     for (; IRight <iLen - . 1 ; IRight ++ ) 
    { 
        IF (aiArray [IRight] < iRootVal) 
        { 
            return  to false ; 
        } 
    } 

    // 3. Analyzing the left subtree is not a binary search tree 
    BOOL bLeft = to true ;
     IF (lLeft> 0 ) 
    { 
        bLeft = BinaryTreePostTraval (aiArray, lLeft); 
    } 

    // 4. Analyzing the right subtree is not a binary search tree 
    BOOL BRIGHT = to true ;
     IF (IRight> 0  )
    {
        bRight = BinaryTreePostTraval(aiArray, bRight);
    }

    return bLeft && bRight;
}

void VerifySquenceOfBSTTestFunc()
{
    cout << "\n\n --------------- VerifySquenceOfBSTTestFunc Start -------------->" << endl;

    int aiArray[] = {8, 6, 10, 5, 7, 9, 11};
    int aiArray2[] = {5, 7, 6, . 9, . 11 , 10 , . 8 }; 

    int iLen = the sizeof (aiArray) / the sizeof ( int ); 
    TRAVERSAL_ARRAY (aiArray, iLen); 

    // Analyzing subsequent array is not sorted binary search tree 
    BOOL bFlag = BinaryTreePostTraval (aiArray, iLen );
     IF (bFlag) 
    { 
        COUT << " TRUE " << endl; 
    } 
    the else 
    { 
        COUT << " FALSE " << endl; 
    }

    TRAVERSAL_ARRAY(aiArray2, iLen);
    bFlag = BinaryTreePostTraval(aiArray2, iLen);
    if (bFlag)
    {
        cout << "TRUE" << endl;
    }
    else
    {
        cout << "FALSE" << endl;
    }
    cout << "\n\n --------------- VerifySquenceOfBSTTestFunc End -------------->" << endl;

}

Guess you like

Origin www.cnblogs.com/yzdai/p/11258730.html