OFFER prove safety traversal sequence from the sequence of binary search trees (nine degrees OJ1367)

Subject description:

Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

 

Input:

Each test case includes two lines:

Conduct a first integer n (1 <= n <= 10000), represents the length of the array.

The second line contains n integer representing the array, the array number range is [0,100000000].

 

Output:

Corresponding to each test case, if the input array is the result of post-order traversal Yes output of a binary search tree, otherwise the output No.

 

Sample input:
7
5 7 6 9 11 10 8
4
7 4 6 5

 

Sample output:
Yes
No

Problem-solving ideas:

  First, we observe Topic: binary search tree traversal sequence after two knowledge points.

  Binary search tree for the search, so the internal node has no duplicate elements . In addition, to meet the nature of the binary tree, the left subtree than their own small, right subtree than their larger. So imagine, if traversed in post-order, first left and right in order of their last traversing the tree, the last element of the array must be themselves (the parent node), and the remaining part is divided into two portions, the first portion than own small (left subtree section), the second part than their own large (right subtree part), so apply this relationship can check whether the cycle is the sequence of binary search tree traversed.

int isPost(int i,int j){
    if(i == j)
        return 1;
    else{
        int k=j-1;
        while(k>=i){
            if(test[k] > test[j])
                k--;
            else
                break;
        }
        int flag = k;
        while(k>=i){
            if(test[k] < test[j])
                k--;
            else
                break;
        }
         
        if(k == i-1){
            if(test[i]>test[j] || test[j-1]<test[j] ){
                //printf("(%d-%d)\n",i,j-1);
                return isPost(i,j-1);
            }else{
                //printf("(%d-%d)(%d-%d)\n",i,flag,flag+1,j-1);
                return (isPost(i,flag))&&(isPost(flag+1,j-1));
            }
        }else{
            return 0;
        }
    }
}

  Also note that the binary tree this question is not full binary , it may appear the following test code:

3

1 2 3

 

In other words, the parent tree, only the left sub-tree, there is no right subtree (right child) , but this test is still to be adopted. Plus a judge is required to determine whether the child is only a single sub-tree

if(test[i]>test[j] || test[j-1]<test[j] ){
                //printf("(%d-%d)\n",i,j-1);
                return isPost(i,j-1);
            }

 

All Code:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int test[10005]={0};
int isPost(int i,int j);
int main(){
    int n,i;
    while(scanf("%d",&n)!=EOF){
        for(i=0;i<n;i++)
            scanf("%d",&test[i]);
        if(isPost(0,n-1))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}
int isPost(int i,int j){
    if(i == j)
        return 1;
    else{
        int k=j-1;
        while(k>=i){
            if(test[k] > test[j])
                k--;
            else
                break;
        }
        int flag = k;
        while(k>=i){
            if(test[k] < test[j])
                k--;
            else
                break;
        }
         
        if(k == i-1){
            if(test[i]>test[j] || test[j-1]<test[j] ){
                //printf("(%d-%d)\n",i,j-1);
                return isPost(i,j-1);
            }else{
                //printf("(%d-%d)(%d-%d)\n",i,flag,flag+1,j-1);
                return (isPost(i,flag))&&(isPost(flag+1,j-1));
            }
        }else{
            return 0;
        }
    }
}
/**************************************************************
    Problem: 1367
    User: xhalo
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:952 kb
****************************************************************/

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545409

Guess you like

Origin blog.csdn.net/weixin_34258782/article/details/91989895