【专题复习1:树的遍历】1086、1053、1079、1090、1094、1106、1119、1138、1143、1151

1086

1086
发现一个问题,如果vector事先resize了的话,再用push_back()添加元素就会失败,这种情况应该像数组那样处理。All in all,要么不resize然后push_back();要么resize用下标添加。

点击查看代码
#include <bits/stdc++.h>

using namespace std;
struct node
{
    int data;
    node* lc;
    node* rc;
};
vector<int> pre,in;
int n;
node* create(int il,int ir,int pl,int pr)
{
    if(pl>pr) return nullptr;
    node* root=new node;
    root->data=pre[pl];
    int k=0;
    while(in[k]!=pre[pl]) k++;
    root->lc=create(il,k-1,pl+1,pl+k-il);
    root->rc=create(k+1,ir,pl+k-il+1,pr);
    return root;
}
int num=0;
void postOrder(node* root)
{
    if(root==nullptr) return;
    postOrder(root->lc);
    postOrder(root->rc);
    cout<<root->data;
    num++;
    if(num<n) cout<<" ";
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    string s;
    int x,inx=0,index=0;
    stack<int> sk;
    cin>>n;
    //pre.resize(n);
    in.resize(n);
    for(int i=0;i<2*n;i++){
        cin>>s;
        if(s=="Push"){
            cin>>x;
            //pre[inx++]=x;
            pre.push_back(x);
            sk.push(x);
        }else{
            in[index++]=sk.top();
            sk.pop();
        }
    }
    node* root=create(0,n-1,0,n-1);
    postOrder(root);
    return 0;
}

1053

1053
update:如果子节点有相同值时原先的算法将会出错,参见

点击查看代码
#include <cstdio>
#include <iostream> // 不知道为什么不用这个头文件PAT会对greater报错
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 110;
struct node {
    int weight;
    vector<int> child;
} Node[maxn];

int n, m, s;
vector<int> path; //路径
vector<vector<int>> ans; //存放所有路径

void DFS(int index, vector<int> path, int sum) {
    if (sum > s) return;
    if (sum == s) {
        if (Node[index].child.size() != 0) return; //还未到叶子结点
        ans.push_back(path);
        return;
    }
    for (int i = 0; i < Node[index].child.size(); i++) {
        int child = Node[index].child[i];
        path.push_back(Node[child].weight);
        DFS(child, path, sum + Node[child].weight);
        path.pop_back();
    }
}

int main() {
    scanf("%d %d %d", &n, &m, &s);
    for (int i = 0; i < n; i++) {
        scanf("%d", &Node[i].weight);
    }
    int id, k, child;
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &id, &k);
        for (int j = 0; j < k; j++) {
            scanf("%d", &child);
            Node[id].child.push_back(child);
        }
    }
    path.push_back(Node[0].weight);
    DFS(0, path, Node[0].weight);
    //对每种情况按字典序从大到小排序
    sort(ans.begin(), ans.end(), greater<vector<int>>());
    for (int i = 0; i < ans.size(); i++) {
        printf("%d", ans[i][0]);
        for (int j = 1; j < ans[i].size(); j++) {
            printf(" %d", ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}

1079

1079

点击查看代码
#include <bits/stdc++.h>

using namespace std;
struct node
{
    int data;
    vector<int> child;
};
vector<node> v;
int n;
double p,r,ans;
void dfs(int index,int depth)
{
    if(v[index].child.size()==0){
        ans+=v[index].data*pow(1+r,depth);
        return ;
    }
    for(int i=0;i<v[index].child.size();i++)
        dfs(v[index].child[i],depth+1);
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int num,c;
    cin>>n>>p>>r;
    r/=100;
    v.resize(n);
    for(int i=0;i<n;i++){
        cin>>num;
        if(num==0) cin>>v[i].data;
        else{
            for(int j=0;j<num;j++){
                cin>>c;
                v[i].child.push_back(c);
            }
        }
    }
    dfs(0,0);
    printf("%.1f\n",ans*p);
    return 0;
}

1090

1090

点击查看代码
#include <bits/stdc++.h>

using namespace std;

vector<int> v[100010];
int n,root,maxnum,maxdepth;
double p,r;
void dfs(int index,int depth)
{
    if(v[index].size()==0){
        if(maxdepth==depth) maxnum++;
        else if(depth>maxdepth){
            maxdepth=depth;
            maxnum=1;
        }
        return ;
    }
    for(int i=0;i<v[index].size();i++)
        dfs(v[index][i],depth+1);
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int temp;
    cin>>n>>p>>r;
    r/=100;
    for(int i=0;i<n;i++){
        cin>>temp;
        if(temp!=-1)
            v[temp].push_back(i);
        else root=i;
    }
    dfs(root,0);
    printf("%.2f %d\n",p*pow(1+r,maxdepth),maxnum);
    return 0;
}

1094

1094

点击查看代码
#include <bits/stdc++.h>

using namespace std;

vector<int> v[100010];
int n,m;
int book[100];
void dfs(int index,int depth)
{
    book[depth]++;
    for(int i=0;i<v[index].size();i++)
        dfs(v[index][i],depth+1);
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int index,num,temp;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>index>>num;
        for(int j=0;j<num;j++){
            cin>>temp;
            v[index].push_back(temp);
        }
    }
    dfs(1,1);
    int maxnum=0,maxdepth=0;
    for(int i=1;i<100;i++){
        if(book[i]>maxnum){
            maxdepth=i;
            maxnum=book[i];
        }
    }
    cout<<maxnum<<" "<<maxdepth;
    return 0;
}

1106

1106

点击查看代码
#include <bits/stdc++.h>

using namespace std;

vector<int> v[100010];
int n;
double p,r;
int minnum=1,mindepth=100000000;
void dfs(int index,int depth)
{
    if(mindepth<depth) return;
    if(v[index].size()==0){
        if(depth<mindepth){
            mindepth=depth;
            minnum=1;
        }else if(depth==mindepth)
            minnum++;
    }
    for(int i=0;i<v[index].size();i++)
        dfs(v[index][i],depth+1);
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int num,temp;
    cin>>n>>p>>r;
    for(int i=0;i<n;i++){
        cin>>num;
        for(int j=0;j<num;j++){
            cin>>temp;
            v[i].push_back(temp);
        }
    }
    dfs(0,0);
    printf("%.4f %d",p*pow(1+r/100,mindepth),minnum);
    return 0;
}

1119

1119

点击查看代码
#include <bits/stdc++.h>

using namespace std;
vector<int> in,pre,post;
bool flag=true;
void getIn(int prel,int prer,int postl,int postr)
{
    if(prel==prer){
        in.push_back(pre[prel]);
        return;
    }
    if(pre[prel]==post[postr]){
        int i=prel+1;
        while(i<=prer&&pre[i]!=post[postr-1]) i++;
        if(i-prel>1)
            getIn(prel+1,i-1,postl,postl+i-prel-1-1);
        else
            flag=false;
        in.push_back(post[postr]);
        getIn(i,prer,postl+i-prel-1,postr-1);
    }
}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n;
    cin>>n;
    pre.resize(n);post.resize(n);
    for(int i=0;i<n;i++) cin>>pre[i];
    for(int i=0;i<n;i++) cin>>post[i];
    getIn(0,n-1,0,n-1);
    printf("%s\n%d",flag==true?"Yes":"No",in[0]);
    for(int i=1;i<n;i++)
        printf(" %d",in[i]);
    printf("\n");
    return 0;
}

1138

1138

点击查看代码
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Node {
    int val;
    Node *left, *right;

    Node(int data) : val(data), left(NULL), right(NULL) {};
};

vector<int> in, pre;
bool flag = 0;

Node *creat(int prel, int prer, int inl, int inr) {
    if (prel > prer)return NULL;
    Node *root = new Node(pre[prel]);
    int k;
    for (k = inl; k <= inr && in[k] != pre[prel]; k++);
    int numleft = k - inl;
    root->left = creat(prel + 1, prel + numleft, inl, k - 1);
    root->right = creat(prel + numleft + 1, prer, k + 1, inr);
    return root;
}

void post(Node *root) {
    if (root == NULL)return;
    post(root->left);
    post(root->right);
    if (flag)return;
    printf("%d", root->val);
    flag = true;
}

int main() {
    int n;
    scanf("%d", &n);
    in.resize(n);
    pre.resize(n);
    for (int i = 0; i < n; i++)scanf("%d", &pre[i]);
    for (int i = 0; i < n; i++)scanf("%d", &in[i]);
    Node *root = creat(0, n - 1, 0, n - 1);
    post(root);
    return 0;
}

1143

1143
还是不太理解。。。
尝试理解这段话==

由于,二叉排序树性质,最近祖先的值一定是介于两者之间的.
根据前序遍历顺序来找节点的原因是:
前序遍历的特点是根、左子树、右子树。根据前序遍历的顺序,对于当前根来说:
1.如果a,b分布在当前根的两侧或者根是a|b,那么答案就找到
2.如果a,b分布在当前根的左侧,由于根左右,下一个点就是左子树的根,顺理成章
3.如果a,b分布在当前根的右侧,根左右,整个左子树都小于a,b,都会不出结果,等到遍历到右子树就行

点击查看代码
#include <bits/stdc++.h>

using namespace std;
map<int,bool> mp;
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int m,n,u,v,a;
    cin>>m>>n;
    vector<int> pre(n);
    for(int i=0;i<n;i++){
        cin>>pre[i];
        mp[pre[i]]=true;
    }
    for(int i=0;i<m;i++){
        cin>>u>>v;
        for(int j=0;j<n;j++){
            a=pre[j];
            if(a>u&&a<v||a>v&&a<u||a==u||a==v) break;
        }
        if(mp[u]==false&&mp[v]==false)
            printf("ERROR: %d and %d are not found.\n", u, v);
        else if(mp[u]==false||mp[v]==false)
            printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
        else if(a==u||a==v)
            printf("%d is an ancestor of %d.\n", a, a == u ? v : u);
        else
            printf("LCA of %d and %d is %d.\n", u, v, a);
    }
    return 0;
}

1151

1151

点击查看代码
#include <bits/stdc++.h>

using namespace std;
map<int,int> pos;
vector<int> in,pre;
void lca(int inl,int inr,int preRoot,int a,int b)
{
    if(inl>inr) return;
    int inRoot=pos[pre[preRoot]],aIn=pos[a],bIn=pos[b];
    if(aIn<inRoot&&bIn<inRoot)
        lca(inl,inRoot-1,preRoot+1,a,b);
    else if((aIn<inRoot&&bIn>inRoot)||(aIn>inRoot&&bIn<inRoot))
        printf("LCA of %d and %d is %d.\n", a, b, in[inRoot]);
    else if(aIn>inRoot&&bIn>inRoot)
        lca(inRoot+1,inr,preRoot+1+(inRoot-inl),a,b);
    else if(aIn==inRoot)
        printf("%d is an ancestor of %d.\n", a, b);
    else if(bIn==inRoot)
        printf("%d is an ancestor of %d.\n", b, a);

}
int main()
{
    #ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int m,n,a,b;
    cin>>m>>n;
    in.resize(n+1);pre.resize(n+1);
    for(int i=1;i<=n;i++){
        cin>>in[i];
        pos[in[i]]=i;
    }
    for(int i=1;i<=n;i++) cin>>pre[i];
    for(int i=0;i<m;i++){
        cin>>a>>b;
        if(pos[a]==0&&pos[b]==0)
            printf("ERROR: %d and %d are not found.\n", a, b);
        else if(pos[a]==0||pos[b]==0)
            printf("ERROR: %d is not found.\n", pos[a] == 0 ? a : b);
        else
            lca(1,n,1,a,b);
    }
    return 0;
}

posted @ 2022-02-26 11:45  勇往直前的力量  阅读(281)  评论(0编辑  收藏  举报