Binary face questions: before the seek subsequent sequence, the subsequent request to the preamble

In the interview, can not avoid the questions will encounter some surface data structure, and today we take a look at the classic face questions binary tree:

Sequence preorder traversal of a binary tree is known ABCDEGHF, inorder traversal order DBAGEHCF, seeking postorder traversal of the binary tree.

and also:

Known binary tree preorder order DBAGEHCF, postorder traversal order DBGHEFCA, seeking a preorder traversal of the binary tree.

How to face similar questions should deal with it?

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

What is a binary tree?

Before we begin, let me again a few nagging: What is the binary tree? Binary Tree (Binary Tree) is a special kind of tree, each node of the tree up to the tree structure of the two sub-trees, each parent node that is grow up to two branches. Typically two child nodes is called a left child node and the right child node. such as:

Wherein, A is the whole root node of a binary tree, then B is the left child node of A, C is the right child node A. Each node, we can implement the Java language:

/**
* 二叉树结点
*/
public class TreeNode {
    public char value;
    /**
    * 左子树
    */
    public TreeNode left;
    /**
    * 右子树
    */
    public TreeNode right;
}

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Three kinds of traversal

Prior to problem-solving, first preorder, preorder, postorder traversal of these three ways it clear lay a good foundation for solving after.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Preorder

Preorder is the first visit the root node, and then visit the left child node, and then access the right child node. Sequence preorder traversal of the binary tree is FIG ABCDEGHF. We can implement the Java language:

public void preOrder(TreeNode biTree) {
    System.out.println(biTree.value);
    if(biTree.left != null) {
        preOrder(biTree.left);
    }
    if(biTree.right != null) {
        preOrder(biTree.right);
    }
}

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Preorder

Preorder is the first visit left child node, and then access the root node, and then access the right child node. Sequence preorder on FIG binary tree is DBAGEHCF. We can implement the Java language:

public void preOrder(TreeNode biTree) {
    if(biTree.left != null) {
        preOrder(biTree.left);
    }
    System.out.println(biTree.value);
    if(biTree.right != null) {
        preOrder(biTree.right);
    }
}

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Postorder

Preorder is the first visit left child node, and then access the right child node, and then access the root node. On the order of FIG postorder binary tree is DBGHEFCA. We can implement the Java language:

public void preOrder(TreeNode biTree) {
    if(biTree.left != null) {
        preOrder(biTree.left);
    }
    System.out.println(biTree.value);
    if(biTree.right != null) {
        preOrder(biTree.right);
    }
}

After the above description, it can be concluded: preorder traversal, in order traversal, postorder traversal of the three kinds of "first", "middle" and "rear" refer to the access order of the root node , "first" is it is to access the root node, "in" is at about the middle of the root access ,, "after" is the last root access.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Order traversal sequence is known before seeking the sequence preorder

Pulled so much, or back to the first pavement just questions of:

Sequence preorder traversal of a binary tree is known ABCDEGHF, inorder traversal order DBAGEHCF, seeking postorder traversal of the binary tree.

Our problem-solving ideas is to first re-constructed according to pre-order traversal order in a binary tree, then according to the write binary tree traversal sequence order.

Welcome attention to micro-channel public number: Wan Cat Society , a shared Java technology dry every week.

Reconstruction of Binary Tree

Preorder traversal ( A BCDEGHF) in the first root node A affirmative, then in order traversal (DB A GEHCF), the left side of the node A (DB) of the node A affirmative left subtree right node a (GEHCF) affirmative right subtree of the node a, is such that the initial binary determination:

A look at the left subtree (DB), pre-order traversing (A B CDEGHF), B is at the top, indicating that subtree root node B, look preorder (D B AGEHCF), B the left (D) is definitely left subtree a, B on the right (no) is certainly right for the a sub-tree, binary tree preliminary judgment is this:

再看A的右子树(GEHCF),在前序遍历(ABCDEGHF)中,C在最前面,说明C为该子树的根结点,再看中序遍历(DBAGEHCF),C的左边(GEH)肯定为C的左子树,C的右边(F)肯定为C的右子树,初步判断二叉树是这样的:

再看C的左子树(GEH),在前序遍历(ABCDEGHF)中,E在最前面,说明E为该子树的根结点,再看中序遍历(DBAGEHCF),E的左边(G)肯定为E的左子树,E的右边(H)肯定为E的右子树,可以最终判断出二叉树是这样的:

欢迎关注微信公众号:万猫学社,每周一分享Java技术干货。

写出后序遍历顺序

这个步骤就比较容易了,根据二叉树得到的后序遍历顺序就是:DBGHEFCA

欢迎关注微信公众号:万猫学社,每周一分享Java技术干货。

已知中后序遍历顺序,求前序遍历顺序

扯了这么多,还是回到刚刚的第一道面试题上:

已知二叉树的中序遍历顺序为DBAGEHCF,后序遍历顺序为DBGHEFCA,求该二叉树的前序遍历。

我们的解题思路是,先根据中后序遍历顺序重新构造出这个二叉树,再根据二叉树写出前序遍历顺序。

欢迎关注微信公众号:万猫学社,每周一分享Java技术干货。

重构二叉树

后序遍历(DBGHEFCA)中的最后一个结点A肯定为根结点,那么在中序遍历(DBAGEHCF)中,A结点的左边(DB)肯定为结点A的左子树,A结点的右边(GEHCF)肯定为结点A的右子树,初步判断二叉树是这样的:

再看A的左子树(DB),在后序遍历(DBGHEFCA)中,B在最后面,说明B为该子树的根结点,再看中序遍历(DBAGEHCF),B的左边(D)肯定为A的左子树,B的右边(无)肯定为A的右子树,初步判断二叉树是这样的:

再看A的右子树(GEHCF),在后序遍历(DBGHEFCA)中,C在最后面,说明C为该子树的根结点,再看中序遍历(DBAGEHCF),C的左边(GEH)肯定为C的左子树,C的右边(F)肯定为C的右子树,初步判断二叉树是这样的:

再看C的左子树(GEH),在后序遍历(DBGHEFCA)中,E在最后面,说明E为该子树的根结点,再看中序遍历(DBAGEHCF),E的左边(G)肯定为E的左子树,E的右边(H)肯定为E的右子树,可以最终判断出二叉树是这样的:

欢迎关注微信公众号:万猫学社,每周一分享Java技术干货。

写出后序遍历顺序

这个步骤就比较容易了,根据二叉树得到的前序遍历顺序就是:ABCDEGHF

欢迎关注微信公众号:万猫学社,每周一分享Java技术干货。

Guess you like

Origin www.cnblogs.com/heihaozi/p/12198659.html