(Java) Construct binary tree OJ question (LeetCode105 constructs binary tree according to preorder and inorder, LeetCode106 constructs binary tree according to postorder and inorder)

content

1. Construct binary tree according to preorder and inorder

2. Construct binary tree according to postorder and inorder


1. Construct binary tree according to preorder and inorder

Construct a binary tree based on preorder and inorder traversal

Question: Given the preorder and inorder traversal of a tree,  inorder,please construct a binary tree and return its root node.

E.g:

You can click on the above link to view the topic, the specific method is as follows:

Analysis: The root node can be obtained from the preorder traversal, and the left and right subtrees of the follow node can be obtained from the inorder. When we construct a binary tree, we find the root from the preorder, and then find the left and right subtrees of the root in the inorder first. Create the root node and then create the left subtree of the root and the right subtree of the root respectively. This process is a recursion, and each recursion must determine which interval in the inorder to find a new root.

Note: The preorder sequence is root---left---right, so when restoring, it is root---left---right, so that index++ is established, index is the root of the preorder mark, and index starts from the beginning of the preorder go to the end

Specific steps:

1. Find the root in preorder traversal

2. Find the root in in-order traversal, mark the demarcation point with pos, the left and right parts of pos are recursive left search and recursive right search

3. First restore the root node, then recursively restore the left subtree of the root, and recursively restore the right subtree of the root

4. Recursive restoration is based on the range divided by in-order traversal

Reference Code:

class Solution {
    int index = 0;  //标记前序的根
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return reBuildTree(preorder,inorder,0,inorder.length);
    }
    public TreeNode reBuildTree(int[] preorder,int[] inorder,int left,int right){
        //递归返回条件
        if(index>=preorder.length || left>=right){
            return null;
        }
        int pos = left;
        //在中序中找根的位置
        while(pos < right){
            if(inorder[pos] == preorder[index]){
                break;
            }
            pos++;
        }
        TreeNode root = new TreeNode(preorder[index]);//还原根
        index++;
        root.left = reBuildTree(preorder,inorder,left,pos);//递归还原左
        root.right = reBuildTree(preorder,inorder,pos+1,right);//递归还原右
        return root;
    }
}

2. Construct binary tree according to postorder and inorder

Construct binary tree from postorder and inorder

Question: Construct a binary tree based on in-order traversal and post-order traversal of a tree.

E.g:

​ 

You can click on the above link to view the topic, the specific method is as follows:

Analysis: Post-order traversal can determine the root node, and in-order traversal can obtain the left and right subtrees of the root node, so we also find the position of the root node in the in-order traversal, mark it with pos, and divide the in-order traversal into two parts. In these two parts, the left subtree of the root and the right subtree of the root are recursively constructed respectively.

Note: The subsequent traversal is left---right---root, so we restore backwards when restoring, and the restoration order is: root---right---left, so that index-- is established, and index is the mark subsequent the root, it goes from the end of the following to the beginning

Specific steps:

1. Find the root from the subsequent traversal

2. Find the root position in the inorder, and use the pos mark to divide the inorder traversal into two parts

3. Recursively restore the right subtree of the root at the right part of the pos token

4. Recursively restore the left subtree of the root at the left part of the pos token

Reference Code:

class Solution {
    int index;  //标记后序遍历的根
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        index = postorder.length-1; //后续的根
        return reBuildTree(inorder,postorder,0,inorder.length);
    }
    public TreeNode reBuildTree(int[] inorder,int[] postorder,int left,int right){
        //递归返回条件
        if(index < 0 || left >= right){
            return null;
        }
        //在中序遍历中找到根的位置
        int pos = left;
        while(pos < right){
            if(postorder[index] == inorder[pos]){
                break;
            }
            pos++;
        }
        //还原根
        TreeNode root = new TreeNode(postorder[index]);
        index--;
        root.right = reBuildTree(inorder,postorder,pos+1,right); //还原根的右
        root.left = reBuildTree(inorder,postorder,left,pos);  //还原根的左
        return root;
    }
}

Guess you like

Origin blog.csdn.net/qq_58710208/article/details/121799006