首页 > 其他分享 >PAT甲级:1174 Left-View of Binary Tree

PAT甲级:1174 Left-View of Binary Tree

时间:2023-10-29 21:44:05浏览次数:41  
标签:pre Binary p0 1174 PAT deal int Tree include

 

题目:1174 Left-View of Binary Tree  25分

 

题解:层次遍历输出每一行最左边的元素。(最开始以为输出部分节点的左子树...想不到思路)

using namespace std;
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cctype>
#include <climits>
#include <set>
#include <stack>
#include <cstring>
#include <sstream>
#include <unordered_map>


// 从上到下输出每一行最左边那个结点
vector<int> in,pre,ans;
map<int,int> pos,mp;
int Tree[25][2],root;
struct node{
    int nodeid,level;
};
void deal(int &idx,int i0,int i1,int p0,int p1){
    if(i0>i1){
        return;
    }
    idx = p0;
    int i = pos[pre[p0]];
    int Llen = i - i0;
    deal(Tree[idx][0],i0,i-1,p0+1,p0+Llen);
    deal(Tree[idx][1],i+1,i1,p0+1+Llen,p1);
}
void bfs(){
    queue<node> q;
    q.push(node{root,1});
    while(!q.empty()){
        node t = q.front();
        // cout << "level: " << pre[t.nodeid] << endl;
        q.pop();
        int tl = t.level;
        if(mp.find(tl) == mp.end()){
            ans.push_back(t.nodeid);
            mp[tl] ++;
        }
        int lid = Tree[t.nodeid][0],rid = Tree[t.nodeid][1];
        if(lid){
            q.push(node{lid,t.level+1});
        }
        if(rid){
            q.push(node{rid,t.level+1});
        }
    }
}
void ex1174(){
    int n;cin >> 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];      
    }
    deal(root,1,n,1,n);
    bfs();
    for(int i=0;i<ans.size();i++){
        cout << pre[ans[i]];
        if(i+1 != ans.size()){
            cout << " ";
        }
    }
}

int main(){
    ex1174();

    return 0;
}

 

标签:pre,Binary,p0,1174,PAT,deal,int,Tree,include
From: https://www.cnblogs.com/jinziguang/p/17796568.html

相关文章

  • PAT_A1081 Rational Sum
    Given N rationalnumbersintheform numerator/denominator,youaresupposedtocalculatetheirsum.InputSpecification:Eachinputfilecontainsonetestcase.Eachcasestartswithapositiveinteger N (≤100),followedinthenextline N rationaln......
  • PAT 甲级【1015 Reversible Primes】
    考察素数判断考察进制转换importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOException{StreamTok......
  • PAT甲级【1014 Waiting in Line】
    考察双向链表importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;importjava.util.LinkedList;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOExcepti......
  • PAT 甲级【1013 Battle Over Cities】
    本题就是dfs.连通图个数-2;但是java慢,最后一个case超时importjava.io.*;importjava.util.HashSet;importjava.util.Set;publicclassMain{@SuppressWarnings("uncheck")publicstaticvoidmain(String[]args)throwsIOException{StreamToken......
  • PAT_B1008 数组元素循环右移问题
    一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0​A1​⋯AN−1​)变换为(AN−M​⋯AN−1​A0​A1​⋯AN−M−1​)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?输入格式:每......
  • 无涯教程-Clojure - re-pattern函数
    re-pattern返回java.util.regex.Pattern的实例。然后将其用于其他模式匹配方法。re-pattern-语法(repatternpat)参数   - "pat"是需要形成的pattern。返回值 - 类型为java.util.regex.Pattern的模式对象。re-pattern-示例(nsclojure.examples.example......
  • [Spring框架学习]SSM 整合,使用maven构建项目的时候,启动项目报错class path resource
    错误:classpathresource[config/spring/springmvc.xml]cannotbeopenedbecauseitdoesnotexist错误原因:找不到我的springmvc.xml,在下面web.xml中是我引用路径,网上找到问题classpath指向路径不是resource路径,所以一直找不到我的xml文件,classpath:到你的class路径......
  • PAT_A1049 Counting Ones【困难】
    Thetaskissimple:givenanypositiveinteger N,youaresupposedtocountthetotalnumberof1'sinthedecimalformoftheintegersfrom1to N.Forexample,given N being12,therearefive1'sin1,10,11,and12.InputSpecification:Eac......
  • PAT_A1104 Sum of Number Segments
    Givenasequenceofpositivenumbers,asegmentisdefinedtobeaconsecutivesubsequence.Forexample,giventhesequence{0.1,0.2,0.3,0.4},wehave10segments:(0.1)(0.1,0.2)(0.1,0.2,0.3)(0.1,0.2,0.3,0.4)(0.2)(0.2,0.3)(0.2,0.3,0.4)......
  • PAT 甲级【1012 The Best Rank】
    本题用java极容易超时,提交了好几次才成功另外9088777750,名次应该是12335,不是12334 importjava.io.*;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizer......