zoukankan      html  css  js  c++  java
  • 二叉树遍历相关

    (天梯赛日常坑队友)

    知道中序和后序,输出层次遍历

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 # define ll long long
     4 # define inf 0x3f3f3f3f
     5 const int maxn = 4e5+1000;
     6 int a[maxn],b[maxn];
     7 struct node
     8 {
     9     int  s;
    10     node *left,*right;
    11     node()
    12     {
    13         left=right=NULL;
    14     }
    15 };
    16 int cnt,flag=0;
    17 node *build(node *root,int l,int r)
    18 {
    19     if(flag)
    20         return NULL;
    21     root=new node;
    22     root->s=b[--cnt];
    23     int i;
    24     int k=0;
    25     for( i=l; i<r; i++)
    26     {
    27         if(a[i]==root->s)
    28         {
    29             k=1;
    30             break;
    31         }
    32     }
    33     if(k==0)
    34     {
    35         flag=1;
    36         return NULL;
    37     }
    38     if(i<r-1)
    39         root->right=build(root->right,i+1,r);
    40     if(i>l)
    41         root->left=build(root->left,l,i);
    42     return root;
    43 }
    44 void print(node *root)
    45 {
    46     queue<node*>q;
    47     int k=0;
    48     q.push(root);
    49     node *tmp=new node;
    50     while(!q.empty())
    51     {
    52         tmp=q.front();
    53         q.pop();
    54         if(tmp)
    55         {
    56             if(k==0)
    57             {
    58                 k=1;
    59                 printf("%d",tmp->s);
    60             }
    61             else
    62                 printf(" %d",tmp->s);
    63             q.push(tmp->left);
    64             q.push(tmp->right);
    65         }
    66     }
    67 }
    68 int main()
    69 {
    70     int n;
    71     scanf("%d",&n);
    72     for(int i=0; i<n; i++)
    73         scanf("%d",&b[i]);
    74     for(int i=0; i<n; i++)
    75         scanf("%d",&a[i]);
    76     cnt=n;
    77     node *root=NULL;
    78     root=build(root,0,n);
    79     print(root);
    80     printf("
    ");
    81 }
    View Code

    知道前序和中序,输出

    AC代码:

     1 node *build( node *root,int left,int right)
     2 {
     3     int i;
     4     int t = a[cnt++];
     5     for(  i = left ; i < right ; i++ )
     6     {
     7         if( b[i] == t )
     8             break;
     9     }
    10     root = new node();
    11     root->index = t;
    12     if( i > left && i < right )
    13         root->left = build(root->left,left,i);
    14     if( i >= left && i <right-1 )
    15         root->right = build(root->right,i+1,right);
    16     return root;
    17 }
    View Code

    知道前序和后序,不太好确定一棵二叉树。

  • 相关阅读:
    Liferay安装maven
    html之pre标签
    a标签使用注意事项
    AngularJS学习记录
    页面不能访问,抛出 spring java.lang.IllegalArgumentException: Name for argument type [java.lang.String] 异常
    ant编译的时候,报错文件不存在,以及版本不一致
    Eclipse 更改Java 版本的方法
    总结一下本次准备环境时遇到的问题,以供下次参考
    数据上下文中的AddOrUpdate方法
    推荐一款github管理神器SourceTree
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10548123.html
Copyright © 2011-2022 走看看