5-6 Flute Karlshu (25 points)

Source: Internet
Author: User

5-6 Flute Karlshu (25 points)

Flute Karlshu is a special two-fork tree whose nodes contain two keywords K1 and K2. First, the flute Karlshu is about the K1 's two-fork search tree, that is, all K1 values of the node's left subtree are smaller than the K1 value of the node, and the right subtree is larger. Second, the K2 keyword for all nodes satisfies the order requirement of the priority queue (which may be set to the minimum heap), that is, the K2 value of the node is smaller than the K2 value of all nodes in its subtree. Given a binary tree, determine if the tree is a Cartesian tree. Input Format:

The input first gives the positive integer n (\le≤1000), which is the number of nodes in the tree. Then n rows, each row gives information about a node, including: The K1 value of the node, the K2 value, the left child node number, and the right child node number. Set the node number from 0~ (N-1) Order. If a node does not have a child node, the position is given a -1−1. output Format:

Output Yes if the tree is a Cartesian tree; Input Sample 1:

6
8 5 1
9 40-1-1
ten 0 3
21-1 4
22-1-1
5 35-1-1
Output Example 1:
YES
Input Sample 2:
6
8 5 1
9 40-1-1
ten 0 3
11-1 4
22-1-1
50 35-1-1
Output Example 2:
NO


Ideas:

BST to meet the key value of judgment, according to the sequence of sequential traversal ... It's going to blow up tomorrow, 233333.

#include <stdio.h> #include <string.h> #include <stdlib.h> int pre[1010];
    struct bst{int left;
    int right;
    int K1;
int K2;

}Q[1010];

int flag;
    void Judge (int num) {int left,right;
    if (!flag) return;
        if (q[num].left!=-1) {left=q[num].left;
            if (Q[LEFT].K2&LT;Q[NUM].K2) {flag=0;
        Return
    } Judge (left);
        } if (q[num].right!=-1) {right=q[num].right;
            if (Q[RIGHT].K2&LT;Q[NUM].K2) {flag=0;
        Return
    } Judge (right);
}} int bb[1010],aa[1010];
int num_b;
    void inorder (int num) {if (num==-1) return;
    Inorder (Q[num].left);
    AA[NUM_B]=BB[NUM_B]=Q[NUM].K1;
    num_b++;
Inorder (Q[num].right);
    } int main () {int n,k1,k2,left,right;
    memset (pre,0,sizeof (pre));
    scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%d%d%d%d", &k1,&k2,&left,&right);
        Q[I].K1=K1;
        Q[I].K2=K2;
        Q[i].left=left;
        Q[i].right=right;
        if (left!=-1) pre[left]++;
    if (right!=-1) pre[right]++;
    } int root;
            for (int i=0;i<n;i++) {if (!pre[i]) {root=i;
        Break
    }} flag=1;
    Judge (root);
    num_b=0;
    Inorder (root);
            for (int i=0;i<num_b;i++) {for (int j=i+1;j<num_b;j++) {if (aa[j]<aa[j-1])
                {int temp=aa[j-1];
                AA[J-1]=AA[J];
            Aa[j]=temp;
            }}} for (int i=0;i<num_b;i++) {if (Aa[i]!=bb[i]) {flag=0;
        Break
    }} if (flag) puts ("YES");

    Else puts ("NO");
return 0;
 }







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.