PTA-7-4 is the same whether a binary search tree

The general idea: The preorder way to compare to. (Static tree)

Subject description:

Given a sequence of insertion can be uniquely determined a binary search tree. However, given a binary search tree, but can be obtained from a variety of different insert sequence. 2, respectively, according to a sequence {e.g., 1, 3} and {2, 3, 1} is inserted into an initially empty binary search tree, the same results were obtained. Thus the insertion sequence for the various input, you need to determine whether they are able to generate the same binary search tree.

Input formats:

Comprising a plurality of groups of test data input. Each row of the first data is given two positive integers N (≦ 10) and L , respectively, and the need to check the number of the number sequence of each insertion sequence element. Line 2 gives N space-separated positive integer as an initial insertion sequence. Finally L rows, each row gives N insertion element belonging L a sequence needs to be checked.

For simplicity, we ensure that each sequence is inserted into an N an arrangement. When the read N is 0, the end flag is input, not the set of data processing.

Output formats:

If it generates a binary search tree with the corresponding initial sequence generated by the same sequence each group needs to be checked, output "Yes", otherwise a "No".

Sample input:

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample output:

Yes
No
No

The subject requires knowledge point is inserted traversal binary search tree node .

For general binary tree, we use the following structure of the representative node.

Because of the direct relationship between a given node, in this question, we directly use the static binary tree to tree to save complete information.

struct Node
{
    char data;
    int left, right, layer;
} tree1[11], tree2[11]; // 两棵树分别保存左右两边的信息

Search for a binary tree node insertion, we directly use the nature of binary search can:

Node *insert(Node *&root, int key)
{
    if (root == NULL)
    {
        root = new Node(key);
        return root;
    }
    else if (key < root->data)
    {
        insert(root->left, key);
    }
    else
    {
        insert(root->right, key);
    }
}

When we created two complete binary tree, we will be able to compare two trees by preorder traversal of the way.

It should be noted that the empty tree is the same! , Only need to pay attention to this point is good, we will be able to easily compare out comparison function as follows:

// 先序遍历判断两棵树树否相同
bool Judge(Node *root1, Node *root2)
{
    if (root1 == NULL && root2 == NULL)
        return true; // 空树相同
    if ((root1 == NULL && root2 != NULL) || (root1 != NULL && root2 == NULL))
        return false; // 只要有一个存在,一个不存在就不同
    if (root1->data == root2->data)
    { // 数据相同时,比较左右子树
        bool leftSame = Judge(root1->left, root2->left);
        bool rightSame = Judge(root1->right, root2->right);
        return leftSame && rightSame;
    }else { // 数据不同直接`    1
        return false;
    }
}

Of course, you can also take another way to navigate, but preorder way is the most efficient one kind.

The complete code is as follows:

/*
    Author: Veeupup
    判断是否是同一棵二叉搜索树
 */
#include <iostream>
#include <cstdio>
#include <cstdint>
#include <queue>
using namespace std;

struct Node
{
    int data;
    Node *left, *right;
    Node(int _data) : data(_data), left(NULL), right(NULL) {}
};

int n, l; // 二叉树结点数目,需要检查的序列数

Node *insert(Node *&root, int key)
{
    if (root == NULL)
    {
        root = new Node(key);
        return root;
    }
    else if (key < root->data)
    {
        insert(root->left, key);
    }
    else
    {
        insert(root->right, key);
    }
}

// 构建树
Node *createTree(int n)
{
    Node *root = NULL;
    int key;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &key);
        insert(root, key);
    }
    return root;
}

// 先序遍历判断两棵树树否相同
bool Judge(Node *root1, Node *root2)
{
    if (root1 == NULL && root2 == NULL)
        return true; // 空树相同
    if ((root1 == NULL && root2 != NULL) || (root1 != NULL && root2 == NULL))
        return false; // 只要有一个存在,一个不存在就不同
    if (root1->data == root2->data)
    { // 数据相同时,比较左右子树
        bool leftSame = Judge(root1->left, root2->left);
        bool rightSame = Judge(root1->right, root2->right);
        return leftSame && rightSame;
    }else { // 数据不同直接`    1
        return false;
    }
}

int main()
{
    // freopen("data.txt", "r", stdin);
    while (scanf("%d", &n) != EOF)
    {
        if (n == 0)
            break;
        scanf("%d", &l);
        Node *originTree = createTree(n); // 生成默认的树
        // levelOrder(originTree);
        for (int i = 0; i < l; i++)
        {
            Node* nowRoot = createTree(n);
            // levelOrder(nowRoot);
            if (Judge(originTree, nowRoot))
            {
                printf("Yes\n");
            }
            else
            {
                printf("No\n");
            }
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/veeupup/p/12592684.html