Construct a binary tree from two traversals of a tree

topic

Given two integer arrays  and  where  is a preorder traversal  of a binary tree and  is an inorder traversal  of the same tree , construct a binary tree and return its root node. preorderinorder preorderinorder

Example 1:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] 
Output: [3,9,20,null,null,15,7]

Example 2:

Input: preorder = [-1], inorder = [-1] 
Output: [-1]

hint:

  • 1 <= preorder.length <= 3000
  • inorder.length == preorder.length
  • -3000 <= preorder[i], inorder[i] <= 3000
  • preorder and  inorder have no  repeating  elements
  • inorder Both appear in preorder
  • preorder Guaranteed  to be the preorder traversal sequence of a binary tree
  • inorder  Inorder traversal sequence guaranteed to be a binary tree

Prerequisite knowledge: 

preorder : Preorder traversal (Preorder Traversal is also known as preorder traversal) - visit the root node ---> the left subtree of the root ---> the right subtree of the root.

inorder:Inorder traversal (Inorder Traversal) - the left subtree of the root ---> root node ---> the right subtree of the root

The first node obtained by the pre-order traversal is the root node, so it can be used to determine the position of the root root node in the in-order traversal.

Then according to the in-order traversal, the left side of the root root node is the left tree of this binary tree, and the right side of the root root node is the right tree of this class binary tree to build this class binary tree

Take example 1 as an example to talk about the understanding of the topic

Example 1 input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]

preorder = [   3 , 9, 20, 15, 7 ]    3 is the root node of this binary tree

inorder = [  9 ,   3 15, 20, 7   ] 9 is the left tree of root, and there is only one node, so 9 is the left node of root

The three nodes 15, 20, and 7 are the right tree of the root, and then traverse the left subtree of the root ---> root node ---> the right subtree of the root according to the middle order

The right node of the root is the node 20, the left node of the 20 node is the node 15 , and the right node is the node 7

Draw a picture to demonstrate the above text description


Problem-solving ideas:

According to the node order of the pre-order traversal given by the title, recurse in the in-order traversal to build a binary tree

1. According to the pre-order traversal, find the position of the root node iRoot of the in-order traversal, create the root node, and then determine the positions of iBegin, iEnd, i++;

2. Judge the positions of iBegin and iEnd. When iBegin>iEnd, return null, and the recursion starts to roll back;

2. In the in-order traversal, according to the positions of iRoot, iBegin, and iEnd, construct the left tree and the right tree;

Recurse the above three steps. After the recursion is completed, the binary tree is constructed and returns to the root node root

Note: The use of the nodes obtained by preorder traversal is the soul of the code in this question

Graphic:


problem solving code

class Solution {
    public int i = 0;

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return create_a_binary_tree(preorder, inorder, 0, inorder.length - 1);
    }

    public TreeNode create_a_binary_tree(int[] preorder, int[] inorder, int inBegin, int inEnd) {
        if (inBegin > inEnd) {
            return null;
            //给定的数组int[] preorder, int[] inorder,遍历完了,没有子树了,该节点为空节点,返回null,递归开始回退
        }
        //先根据先序遍历创建根节点
        TreeNode root = new TreeNode(preorder[i]);
        //找到当前根节点,在中序遍历的位置
        int rootIndex = findIndex(inorder, inBegin, inEnd, preorder[i]);
        i++;
        //递归构建左树
        root.left = create_a_binary_tree(preorder, inorder, inBegin, rootIndex - 1);
        //递归构建右树
        root.right = create_a_binary_tree(preorder, inorder, rootIndex + 1, inEnd);
        //前序遍历完成,返回根节点
        return root;
    }

    private int findIndex(int[] inorder, int inBegin, int inEnd, int key) {
        for (int j = inBegin; j <= inEnd; j++) {
            if (inorder[j] == key) {
                return j;
            }
        }
        return -1;
    }
}

operation result

topic link

https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/submissions/

Lift one, flip three, and do one more

Construct a binary tree based on the in-order traversal and post-order traversal of a tree

Given two integer arrays  inorder and  postorder where  inorder is the inorder traversal of the binary tree and  postorder the postorder traversal of the same tree, please construct and return this  binary tree  .

Example 1:

Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 
Output: [3,9,20,null,null,15,7]

Example 2:

Input: inorder = [-1], postorder = [-1] 
Output: [-1]

Postorder traversal (Postorder Traversal) - the left subtree of the root ---> the right subtree of the root ---> the root node.
After creating the root node, first create the right number, and then create the left tree

Explain according to example 1, the following figure is the code recursive process

With the recursion of the code, the process of building the binary tree 

 problem solving code

public class Solution {
    public int i = 0;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        i = postorder.length - 1;
        return create_a_binary_tree(postorder, inorder, 0, inorder.length - 1);
    }

    public TreeNode create_a_binary_tree(int[] postorder, int[] inorder, int inBegin, int inEnd) {
        if (inBegin > inEnd) {
            return null;
            //给定的数组int[] postorder, int[] inorder,遍历完了,没有子树了,该节点为空节点,返回null,递归开始回退
        }
        //先根据先序遍历创建根节点
        TreeNode root = new TreeNode(postorder[i]);
        //找到当前根节点,在中序遍历的位置
        int rootIndex = findIndex(inorder, inBegin, inEnd, postorder[i]);
        i--;
        //递归先构建右树
        root.right = create_a_binary_tree(postorder, inorder, rootIndex + 1, inEnd);
        //递归后构建左树
        root.left = create_a_binary_tree(postorder, inorder, inBegin, rootIndex - 1);
        //前序遍历完成,返回根节点
        return root;
    }

    private int findIndex(int[] inorder, int inBegin, int inEnd, int key) {
        for (int j = inBegin; j <= inEnd; j++) {
            if (inorder[j] == key) {
                return j;
            }
        }
        return -1;
    }
}

operation result

Topic link:

https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/submissions/

Complete flower ✿✿ヽ(°▽°)ノ✿✿

Guess you like

Origin blog.csdn.net/m0_73740682/article/details/132308710