由中序遍历和后序遍历 输出先序遍历

由中序遍历和后序遍历 输出先序遍历

public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String mid=input.nextLine();
        String next=input.nextLine();
        System.out.println(change(mid,next));
    }

    private static String change(String mid, String next) {
        if(mid.length()>0){
            int len=next.length();
            if(len == 1)
                return next;
            if(len <= 0 ||len > 8)
                return "";
            char root=next.charAt(len - 1);
            int rootIndex = mid.indexOf(root);
            return next.charAt(len - 1) + change(mid.substring(0,rootIndex),next.substring(0,rootIndex)) + change(mid.substring(rootIndex+1),next.substring(rootIndex,len - 1));
        }else
            return "";
    }
原创文章 55 获赞 55 访问量 5780

猜你喜欢

转载自blog.csdn.net/weixin_45007916/article/details/105970153