二叉树的遍历-由中序和先序遍历序列确定一颗二叉树

an inorder binary tree traversal  can be implemented in a non-recursive way with a stack

for example 

suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed

the stack operations are :

push(1) push(2) push(3) pop()  pop() push(4) pop() pop() push(5) push(6) pop() pop()

Then a unique binary tree (shown in Figure below) can be generated from this sequence of operations .

your task is to give the postorder traversal sequence of this tree

意思是用栈模拟一颗二叉树的先序和中序遍历过程,求这颗二叉树的后序遍历序列

 

输入 :

6
push 1
push 2
push 3
pop                                                              
pop
push 4
pop
pop
push 5
push 6
pop
pop

输出:

  • 3 4 2 6 5 1

 

Push的次序是1 2 3 4 5 6,所以先序遍历序列是1 2 3 4 5 6

Pop的次序是 3 2 4 1 6 5,所以中序遍历序列是 3 2 4 1 6 5

 

package com.yao;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class TestTreeTraversal {
    static int counts = 0;
    static int sum = 0;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        sum = n;
        Stack<Integer> integerStack = new Stack<>();
        int pre[] = new int[n], in[] = new int[n], preIndex = 0, inIndex = 0;
        for (int i = 0; i < 2 * n; ++i) {
            String[] inputStrings = br.readLine().split("\\s+");
            if (inputStrings[0].equals("Push")) {
                int inStack = Integer.parseInt(inputStrings[1]);
                integerStack.add(inStack);
                pre[preIndex++] = inStack;
            } else {
                int outStack = integerStack.pop();
                in[inIndex++] = outStack;
            }
        }
        TreeNode root = create(pre, 0, preIndex-1,
                in, 0, inIndex-1);
        postOrder(root);
    }

    public static TreeNode create(int[] pre, int preLeft, int preRight,
                                  int[] in, int inLeft, int inRight) {
        if (preLeft > preRight) {
            return null;
        }
        TreeNode root = new TreeNode(pre[preLeft]);
        int findIndex = -1;
        // 找出中序遍历中的根节点位置
        for (int i = inLeft; i <= inRight; ++i) {
            if (in[i] == root.data) {
                findIndex = i;
                break;
            }
        }
        // 计算左子树的数量
        int leftNum = findIndex - inLeft;
        root.left = create(pre, preLeft + 1, preLeft + leftNum,
                in, inLeft, findIndex - 1);
        root.right = create(pre, preLeft + leftNum + 1, preRight,
                in, findIndex + 1, inRight);
        return root;
    }

    public static void postOrder(TreeNode root) {
        if (root!=null) {
            postOrder(root.left);
            postOrder(root.right);
            ++counts;
            if (counts<sum){
                System.out.print(root.data+" ");
            } else {
                System.out.print(root.data);
            }
        }
    }
}
class TreeNode {
    int data;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int data) {
        this.data = data;
    }
}

/**
 * @author
 * @date 2020/11/26 14:30

public class TestTreeTraversal {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int i = Integer.parseInt(scanner.nextLine());

        String[] lst1 = new String[i];
        String[] lst2 = new String[i];

        int index1 = 0;
        int index2 = 0;
        String s = null;
        while (!(s = scanner.nextLine().trim()).isEmpty()){
            String[] s1 = s.split(" ");
            if(s1.length>=2){
                if("push".equals(s1[0])){
                    lst1[index1]=s1[1];
                    index1++;
                }else if("pop".equals(s1[0])){
                    lst2[index2]=s1[1];
                    index2++;
                }
            }
        }

        System.out.println(Arrays.asList(lst1) );
        System.out.println(Arrays.asList(lst2));



    }
}
 */

由于Push的顺序是先序遍历,所以本题可以直接在输入时就进行递归建树

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值