给出中后序遍历,求先序遍历(java)

题目

链接: https://ac.nowcoder.com/acm/contest/19859/N
题意:给出中序和后续遍历,输出先序遍历


输入

BADC
BDCA

输出

ABCD

AC代码 - - -

#solution1

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Node{
    int c,L,R;
    public Node() {;}
    public Node(int c,int l,int r) {
        this.c = c;
        this.L = l;
        this.R = r;
    }
}
public class Main {
    static String s1,s2;
    static int cnt = 0,mx = 100,vis[]=new int[mx];
    static Node node[] = new Node[mx];
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static void init() {
        for(int i = 0; i < mx; ++i) {
            node[i] = new Node(0,-1,-1);
        }
    }
    static String ins() throws IOException{
        String s = "";
        char c = (char)in.read();
        while(c<'A'||c>'Z') c = (char)in.read();
        while(c>='A'&&c<='Z') { s += c; c=(char)in.read();}
        return s;
    }
    static int build(int l,int r,int p) {
        if(l>r||p<0) return -1;
        int mid = 0, ans = 0, pos = 0;
        for(int i = l; i <= r; ++i) {
            if(s2.charAt(p)==s1.charAt(i)) {
                mid = i;
                break;
            }
        }
        node[++cnt].c = s2.charAt(p);//不要标记mid,p,要标记字母
        vis[s2.charAt(p)-'A']=1;
        //System.out.println(mid);
        ans = cnt;
        node[ans].R = build(mid+1,r,p-1);
        int t = node[ans].R;
        for(int i = p; i >= 0; --i) {
            if(vis[s2.charAt(i)-'A']==0) {
                pos = i;
                break;
            }
        }
        //System.out.println(l+" "+(mid-1)+" "+pos);
        node[ans].L = build(l,mid-1,pos);
        return ans;
    }
    static String preorder(int rt) {
        if(rt==-1) return "";
        return (char)node[rt].c+""+preorder(node[rt].L)+preorder(node[rt].R);
    }
    public static void main(String[] args) throws IOException{
        s1 = ins();s2 = ins();
        init();
        int rt = build(0,s1.length()-1,s2.length()-1);
        String res = preorder(rt);
        System.out.println(res);

    }
}


#solution2

//模拟递归建树,这个过程就可以求先序遍历
//递归思想:如下注释
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static String s1,s2;
    static int vis[] = new int[100]
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static String ins() throws IOException{
        String s = "";
        char c = (char)in.read();
        while(c<'A'||c>'Z') c = (char)in.read();
        while(c>='A'&&c<='Z') { s += c; c=(char)in.read();}
        return s;
    }
    
    static String build(int l,int r,int p) {
        if(l>r||p<0) return "";
        int mid = 0, ans = 0, pos = 0;
        for(int i = l; i <= r; ++i) {
            if(s2.charAt(p)==s1.charAt(i)) {
                mid = i;
                break;
            }
        }
        vis[s2.charAt(p)-'A']=1;
        String t2 = build(mid+1,r,p-1);
        for(int i = p; i >= 0; --i) {
            if(vis[s2.charAt(i)-'A']==0) {
                pos = i;
                break;
            }
        }
        String t1 = build(l,mid-1,pos);
        return s2.charAt(p)+""+t1+t2;//建树是先根后左右。字符串拼接,先根左后右
        //*递归思想及细节
        //右子树的范围最小可达[1-3]个节点,拼接也满足先根左后右原则,
        //以此推广到整个右子树都是满足先根左后右的原则,整个左子树也同理
        
    }
    
    public static void main(String[] args) throws IOException{
        s1 = ins();s2 = ins();
        String res = build(0,s1.length()-1,s2.length()-1);
        System.out.println(res);

    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值