zoukankan      html  css  js  c++  java
  • pat 是否同一棵二叉搜索树

    给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。

    输入格式:
    输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。最后L行,每行给出N个插入的元素,属于L个需要检查的序列。

    简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

    输出格式:

    对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。

    输入样例:

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

    输出样例:

    Yes
    No
    No

    思路:
    每组数据中,先建立初始树,之后再在根据检查序列在初始树上大标记。

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node* node;
    
    struct Node
    {
        int Data;
        node Left;
        node Right;
        int Key;
    };
    
    node Insert(node root,int data)
    {
        node re = root;
        node p = (node)malloc(sizeof(struct Node));
        p->Data = data;
        p->Left = NULL;
        p->Right = NULL;
        p->Key = 0;
        if(root == NULL)
        {
            root = p;
            return root;
        }
        else
        {
            while(root)
            {
                if(root->Data > data && root->Left)
                {
                    root = root->Left;
                }
                else if(root->Data < data && root->Right)root = root->Right;
                else break;
            }
            if(root->Data > data)root->Left = p;
            else root->Right = p;
        }
        return re;
    }
    
    int Search(node root,int data)
    {
        while(root && root->Data!=data)
        {
            if(root->Key == 0)return 1;
            if(root->Data>data)root = root->Left;
            else root = root->Right;
        }
        root->Key = 1;
        return 0;
    }
    
    void Ini(node root)
    {
        if(root == NULL)return ;
        if(root->Key)root->Key = 0;
        Ini(root->Left);
        Ini(root->Right);
    }
    
    int board[15];
    
    int main()
    {
        int N,L;
        while(scanf("%d",&N) && N)
        {
            scanf("%d",&L);
            node root = NULL;
            for(int i=0 ; i<N ; i++)
            {
                int mid;
                scanf("%d",&mid);
                root = Insert(root,mid);
            }
    
            while(L--)
            {
                int i;
                for(i=0 ; i<N ; i++)scanf("%d",&board[i]);
                for(i=0  ; i<N ; i++)
                {
                    if(Search(root,board[i]))break;
                }
                Ini(root);
                if(i == N)printf("Yes
    ");
                else printf("No
    ");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
    HTMl5的存储方式sessionStorage和localStorage详解
    浏览器文档模式设置
    页面缓存
    SQL Server死锁总结
    读写分离,读写分离死锁解决方案,事务发布死锁解决方案,发布订阅死锁解决方案
    页面缓存
    HTML5 history API,创造更好的浏览体验
    【转】编写高质量代码改善C#程序的157个建议——建议96:成员应优先考虑公开基类型或接口
    【转】编写高质量代码改善C#程序的157个建议——建议95:避免在构造方法中调用虚成员
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514207.html
Copyright © 2011-2022 走看看