uva-二叉樹的遍歷

代碼測試一定要嚴謹!代碼測試一定要嚴謹!代碼測試一定要嚴謹!
今天就是因爲complete寫成了complet,結果提交了n次,恨死我~
先說下今天做的幾個題目,今天主要做了紫書,有關樹的遍歷的部分題目,熟悉了BFS與DFS的相應操作,然而雖然之前已經學了數據結構的相關知識,現在卻忘的差不多了,所以,菜雞隻好比着紫書上的代碼,一點點的將之前的東西拾起來,所以本文的代碼基本上與紫書一致。不得不說,大佬就是大佬,看人家的代碼竟有一絲享受,實在是妙。

uva-122

這道題目是給定如(12,LLLLL)的序列,判斷所給序列是否合法(即是完整的樹,其各結點值不衝突),若合法則按層次遍歷該序列
使用的技巧:
數據的輸入,使用字符串s輸入的特性,即以\0停止,自然將每個()作爲一次輸入,然後使用strcmp檢驗停止標記空(),再用sscanf提取數值,最後用strchr,提取路徑首位置
直接上代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn =1e5+5;
struct Node
{
    bool have_value;
    int v;
    Node *left,*right;
    Node():have_value(false),left(NULL),right(NULL){}//構造函數
};
Node *root=NULL;
bool failed=false;
Node *newnode()
{
    return new Node();
}
void addNode(int v,char *s)
{
    int n=strlen(s);
    Node *u=root;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='L')
        {
            if(u->left==NULL)
            {
                u->left=newnode();
            }
            u=u->left;
        }
        else if(s[i]=='R')
        {
            if(u->right==NULL)
            {
                u->right=newnode();
            }
            u=u->right;
        }
    }
    if(u->have_value)
        failed=true;
    u->v=v;
    u->have_value=true;
}
void remove_tree(Node* u)//刪除佔用的內存
{
    if(u==NULL)
        return ;
    remove_tree(u->left);
    remove_tree(u->right);
    delete u;
}
bool read_input()
{
    //初始化
    failed=false;
    remove_tree(root);
    root=newnode();
    for(;;)
    {
       char s[maxn];
        if(scanf("%s",s)!=1)//以空白字符爲分隔符
            return false;
        if(!strcmp(s,"()"))
            break;
        int v;
        sscanf(&s[1],"%d",&v);
        addNode(v,strchr(s,',')+1);
    }
    return true;
}
bool bfs(vector<int> &ans)
{
    queue<Node*>q;
    ans.clear();
    q.push(root);
    while(!q.empty())
    {
        Node *u=q.front();
        q.pop();
        if(!u->have_value)
            return false;
        ans.push_back(u->v);
        if(u->left!=NULL)
            q.push(u->left);
        if(u->right!=NULL)
            q.push(u->right);
    }
    return true;
}
int main()
{

    for(;;)
    {
        if(read_input()==false)
            break;
        if(failed)
            printf("not complete\n");
        else
        {
            vector<int> ans;
            if(!bfs(ans))
               printf("not complete\n");
            else
            {
                for(int i=0;i<ans.size();i++)
                {
                    if(i)
                        printf(" ");
                    printf("%d",ans[i]);
                }
                printf("\n");
            }

        }

    }
}

uva548

本題是由中序和後序序列構造樹,然後從構造後的樹中進行dfs選擇符合題意得值

#include<bits/stdc++.h>
using namespace std;
#define maxn 10005
int in_order[maxn]={0};
int post_order[maxn]={0};
int n;
int lch[maxn]={0};
int rch[maxn]={0};
int Min,Min_num;
bool read_list(int *a)
{
    string line ;
    if(!getline(cin,line))
        return false;
    stringstream ss(line);
    n=0;
    int x;
    while(ss>>x)
    {
        a[n++]=x;
    }
    return n>0;
}
int build(int L1,int R1,int L2,int R2)//L1,R1前序起止,L2,R2後序起止
{
    if(L1>R1||L2>R2)
        return 0;
    int root=post_order[R2];
    int p=L1;
    while(in_order[p]!=root)
        p++;
    int cnt=p-L1;//走過的元素
    lch[root]=build(L1,p-1,L2,L2+cnt-1);
    rch[root]=build(p+1,R1,L2+cnt,R2-1);
    return root;
}
void dfs(int u,int sum)
{
    sum+=u;
    if(!lch[u]&&!rch[u])
    {
        if((sum<Min)||(sum==Min&&u<Min_num))
        {
            Min=sum;
            Min_num=u;;
        }
    }
    if(lch[u])
        dfs(lch[u],sum);
    if(rch[u])
        dfs(rch[u],sum);
}
int main()
{
    while(read_list(in_order))
    {
        read_list(post_order);
        Min=1000000;
        Min_num=1000000;
        build(0,n-1,0,n-1);
        dfs(post_order[n-1],0);
        printf("%d\n",Min_num);
        memset(in_order,0,n);
        memset(post_order,0,n);
        memset(lch,0,maxn);
        memset(rch,0,maxn);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章