Sword refers to the post-order traversal sequence of the OFFER Binary Search Tree (9-degree OJ1367)

Source: Internet
Author: User

Description:

Enter an integer array to determine whether the array is the result of a sorted traversal of a binary search tree. If Yes, Yes is output; otherwise, No is output. Assume that any two numbers in the input array are different.

 

Input:

Each test case contains two rows:

The first behavior is an integer n (1 <=n <= 10000), indicating the length of the array.

The second row contains n integers, indicating the array. The value range of the number in the array is [0,].

 

Output:

For each test case, if the input array is the result of a binary search tree's sequential traversal, Yes is output; otherwise, No is output.

 

Sample input:
75 7 6 9 11 10 847 4 6 5

 

Sample output:
YesNo
Solution:

First, let's look at the question: a binary search tree that traverses two knowledge points in descending order.

Binary Search Tree, used for search, soThe internal node does not have repeated elements.. In addition,The left subtree is smaller than itself, and the right subtree is larger than itself.SoIt can be imagined that if we traverse the tree in the descending order, we will traverse the tree in the order of the left and right, and the last element of the array will be itself (parent node), and the remaining part will be divided into two parts, the first part is smaller than itself (left subtree), and the second part is larger than itself (right subtree ), therefore, by applying this relationship, we can cyclically check whether it is a post-order traversal of the Binary Search Tree.

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;        }    }}

Note that,The binary tree in this question is not a full Binary Tree, The following test code may appear:

31 2 3

 

That is to say,The parent node of this tree has only the left subtree, but not the right subtree (the right subtree)But this test case still needs to pass. Therefore, we need to add a judgment to determine whether there is only one child 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****************************************************************/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.