1043. Is It a Binary Search Tree (25)

来源:互联网 发布:catti监控linux主机 编辑:程序博客网 时间:2024/06/05 11:17

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key. 
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key. 
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case.  For each case, the first line contains a positive integer N (<=1000).  Then N integer keys are given in the next line.  All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not.  Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree.  All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
78 6 5 7 10 8 11
Sample Output 1:
YES5 7 6 8 11 10 8
Sample Input 2:
78 10 11 8 6 7 5
Sample Output 2:
YES11 8 10 7 5 6 8
Sample Input 3:
78 6 8 5 10 9 11
Sample Output 3:

NO

解题思路:看到树很容易想到用递归的方法,递归判断树和其子树是不是二叉查找树;镜像树也是同样的办法!关于后序序列;因为后序遍历的顺序为左右根的顺序,所以其逆序为根右左;又因为这边的右和左指的是子树,所以再递归调用,刚好和前面的递归判断一样,所以只要每次递归时记录根节点就OK!

举个例子:第一个测试用例:8、6、5、7、10、8、11

分成8------6、5、7--------10、8、11 所以根右子树左子树的顺序是8、10、11、8、6、7、5 --->逆序后就是5、7、6、8、11、10、8  就是后序序列

AC的参考代码:

#include <iostream>#include <vector>using namespace std;int N;int input[1001];vector<int> u1;     //用来记录后序遍历的序列vector<int> u2;     //用来记录镜像的后序遍历的序列int isBST(int arr[],int start,int last){    //判断是否为二叉查找树    if(start==last+1){      //根只有一边有子树的一种特殊递归结束条件        return 1;    }    u1.push_back(arr[start]);   //记录后序序列    if(start==last){        //一般的递归结束条件        return 1;    }    int i = start+1;    while(arr[i]<arr[start] && i<=last){        i++;            //找到右子树的根节点;注意要加1<=last条件 否则会显示段错误!PAT中 堆栈会爆掉    }    int index = i;      //判断剩下的节点也符合要求    while(index<=last){        if(arr[index]>=arr[start]){            index++;        }else{            return 0;        }    }    int left = 0,right = 0;    right = isBST(arr,i,last);      //判断右子树    left = isBST(arr,start+1,i-1);  //判断左子树    if(left==0 || right==0){        return 0;    }else{        return 1;    }}int isMirrorBST(int arr[],int start,int last){      //判断镜像是否为二叉查找树    if(start==last+1){        return 1;    }    u2.push_back(arr[start]);    if(start==last){        return 1;    }    int i = start+1;    while(arr[i]>=arr[start] && i<=last){        i++;            //找到右子树的根节点    }    int index = i;      //标记右子树根节点    while(index<=last){        if(arr[index]<arr[start]){            index++;        }else{            return 0;        }    }    int left = 0,right = 0;    right = isMirrorBST(arr,i,last);    left = isMirrorBST(arr,start+1,i-1);    if(left==0 || right==0){        return 0;    }else{        return 1;    }}int main(){    cin>>N;    for(int i=0;i<N;i++){        cin>>input[i];    }    int isBSFRight = isBST(input,0,N-1);    int isisMirrorBSTRight = isMirrorBST(input,0,N-1);    if(isBSFRight || isisMirrorBSTRight){        cout<<"YES"<<endl;        if(isBSFRight){            for(int i=u1.size()-1;i>0;i--){                cout<<u1[i]<<" ";            }            cout<<u1[0];        }else if(isisMirrorBSTRight){            for(int i=u2.size()-1;i>0;i--){                cout<<u2[i]<<" ";            }            cout<<u2[0];        }    }else{        cout<<"NO";    }    return 0;}


 

0 0
原创粉丝点击