[Blue Bridge Cup Algorithm] Give binary tree pre-order traversal, in-order traversal, output post-order traversal

1. Problem description

Recently, WYF is preparing to visit his point card factory. The manager of the WYF Group, Cyanide Waste, needed to help WYF design a "visit" route. Now, cyanide garbage knows a few things:
(1) WYF's point card factory constitutes a binary tree.
(2)—There are n factories in total.
(3). He needs to list the points on the tree in a post-order traversal method to draw a map.
Fortunately, recently his subordinates gave him the data of pre-order traversal and in-order traversal. However, the cyanide waste has to help Yu Tanyu to solve some problems recently, and there is no time. Please help him to complete this task for him. Due to some special requirements of cyanide waste, the visit route of WYF will be a post-order traversal of this tree.


2. Problem Analysis

insert image description here
Problem- solving idea : The first character of the preorder traversal sequence is the root node. For inorder traversal, the root node is in the middle of the inorder traversal sequence, the left part is the inorder traversal sequence of the left subtree of the root node, and the right part is the inorder traversal sequence of the right subtree of the root node.


3. Algorithm Implementation

Node类

public class Node {
    
    
    public int data;
    public Node left;
    public Node right;

    public Node() {
    
    
    }
    public Node(int data) {
    
    
        this.data = data;
        this.left=null;
        this.left=null;
    }
}

Tree类

public class Tree {
    
    
    private Node root;
    //构造器
    public Tree() {
    
    
            root=null;
        }

    /**
     * //后序遍历
     * @param localRoot
     */
    public void sendOrder(Node localRoot){
    
    
            if(localRoot!=null){
    
    
                sendOrder(localRoot.left);
                sendOrder(localRoot.right);
                System.out.print(localRoot.data+" ");
            }
        }
        public void sendOrder(){
    
    
            this.sendOrder(this.root);
        }
        public void startTree(int[] boforeOrder, int []inOrder){
    
    
            this.root=this.startTree(boforeOrder, 0, boforeOrder.length-1,inOrder,0,inOrder.length);
        }
        public Node startTree(int[] beforeOrder, int bStart, int bEnd, int[] inOrder, int inStart,
                              int inEnd) {
    
    
            if(bStart > bEnd || inStart > inEnd){
    
    
                return null;
            }
            int root = beforeOrder[bStart];
            Node head=new Node(root);

            //根节点所在的位置
            int rootIndex= searchIndex(inOrder, root, inStart, inEnd);
            //左子树个数
            int sum =rootIndex- inStart -1;
            //构建左子树
            head.left= startTree(beforeOrder, bStart +1, bStart +1+ sum,inOrder, inStart, inStart + sum);
            //构建右子树
            head.right= startTree(beforeOrder, bStart +2+ sum, bEnd,inOrder,rootIndex+1, inEnd);

            return head;
        }

        public int searchIndex(int[] inOrder, int root, int statrt,
                               int end) {
    
    
            for (int i = statrt; i <= end; i++) {
    
    
                if(inOrder[i]==root){
    
    
                    return i;
                }
            }
            return -1;
        }

    }

Main类

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Tree tree=new Tree();

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入工厂数:");
        int n=scanner.nextInt();
        int[] beforeTree= new int[n];
        int[] inTree =new int[n];
        System.out.println("先序遍历:");
        for(int i=0;i<n;i++){
    
    
            beforeTree[i]=scanner.nextInt();
        }
        System.out.println("中序遍历:");
        for(int i=0;i<n;i++){
    
    
            inTree[i]=scanner.nextInt();
        }

        tree.startTree(beforeTree, inTree);
        System.out.println("二叉树的后序遍历:");
        tree.sendOrder();


    }
}

Test
1, 测试用例: n=8
Pre-order traversal: 1 2 4 5 7 3 6 8
In-order traversal: 4 2 7 5 1 8 6 3
测试结果:
Post-order traversal: 4 7 5 2 8 6 3 1
insert image description here

2. 测试用例: n=6
Pre-order traversal: 4 2 6 3 1 5
In-order traversal: 6 2 3 4 1 5
测试结果:
Post-order traversal: 6 3 2 5 1 4
insert image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208152&siteId=291194637