zoukankan      html  css  js  c++  java
  • 查找二叉树(tree_a)

    查找二叉树(tree_a)

    连接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1367
    时间限制: 1000 ms         内存限制: 65536 KB

    【题目描述】

    已知一棵二叉树用邻接表结构存储,中序查找二叉树中值为x的结点,并指出是第几个结点。例:如图二叉树的数据文件的数据格式如下:

     

    【输入】

    第一行n为二叉树的结点个树,n<=100;第二行x表示要查找的结点的值;以下第一列数据是各结点的值,第二列数据是左儿子结点编号,第三列数据是右儿子结点编号。

    【输出】

    一个数即查找的结点编号。

    【输入样例】

    7
    15
    5 2 3
    12 4 5
    10 0 0
    29 0 0
    15 6 7
    8 0 0
    23 0 0

    【输出样例】

    4
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn = 105;
    struct node{
        int data,l,r;
    }p[maxn];
    int d[maxn];
    int n,x;
    int t,ans;
    void rec(int k)
    {
        
        if(p[k].data==x)ans=k;
        if(p[k].l)rec(p[k].l);
        d[k]=++t;
        if(p[k].r)rec(p[k].r);
    }
    int main()
    {
        
        cin>>n>>x;
        int a,b,c;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            p[i].data = a;
            p[i].l = b;
            p[i].r = c;
            
        }
        rec(1);
        cout<<d[ans];
    }
  • 相关阅读:
    create-react-app 修改 webpack output.publicPath
    洛谷 P1282 多米诺骨牌 (01背包)
    UVa 1627
    UVa 1626
    UVa 11584
    UVa 11400
    UVa 116
    UVa 1347 Tour (dp)
    树形背包小结
    数据流图题目一
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8017818.html
Copyright © 2011-2022 走看看