[LeetCode] 105. Construct a binary tree from the preorder and inorder traversal sequences 106. Construct a binary tree from the inorder and postorder traversal sequences

105. Construct Binary Tree from Preorder and Inorder Traversal Sequences

This question is also a classic data structure question. Sometimes interview questions are also encountered. Knowing the traversal sequence of the preorder and the inorder, we can know that the first element is the root node from the preorder traversal, and the inorder traversal The characteristic is that the left side of the root node is all the left subtree, and the right side is all the right subtree, and then traverse the preorder sequence in turn, divide the inorder sequence, and continuously combine these two sequences to write code. Detailed instructions are in the code. Because the preorder is root left and right, and the inorder is left root right.

 

algorithm code

class Solution {
    private int preindex;  //成员变量 是遍历前序数组的索引 弄成成员变量比较好
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }

    public TreeNode buildTreeChild(int[] preorder,int[] inorder,int inleft,int inright){

        if(inleft>inright) return null;  //说明当前节点无左右子节点了
        TreeNode root = new TreeNode(preorder[preindex]);
        int index = find(inorder,preorder[preindex]); //找在中序数组中的索引,用来分组
        preindex++; 
        root.left = buildTreeChild(preorder,inorder,inleft,index-1); //先递归并返回当前节点的左子节点
        root.right = buildTreeChild(preorder,inorder,index+1,inright); //后递归并返回当前节点的右子节点
        return root;  //最后返回当前节点

    }

    public static int find(int[] inorder,int key){ //用来找每个根节点在后序数组中的下标,并返回下标
        int i = 0;
        while(inorder[i]!=key){
            i++;
        }
        return i;
    }
}

 

106. Construct Binary Tree from Inorder and Postorder Traversal Sequences

This question is almost the same as the previous question, the difference is that the inorder and postorder are known, and the postorder is characterized by the last element, which is the root node, so the postorder sequence needs to be traversed from back to front. And the order of recursively returning the left and right subtrees will also change. The rest is the same as the previous code. Because the inorder is the left root and the right, and the postorder is the left and right roots.

 

algorithm code

class Solution {

    private int postindex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postindex = postorder.length-1;  //指向序列最后一个元素,倒序遍历
        return buildTreeChild(postorder,inorder,0,postorder.length-1);

    }

     private TreeNode buildTreeChild(int[] postorder,int[] inorder ,int inleft,int inright){
        if(inleft>inright) return null;
        TreeNode root = new TreeNode(postorder[postindex]);
        int index = find(inorder,postorder[postindex]);
        postindex--;
        root.right = buildTreeChild(postorder,inorder,index+1,inright); //这里有区别
        root.left = buildTreeChild(postorder,inorder,inleft,index-1); //有区别
        return root;

    }
    private static int find(int[] inorder,int key){
        int i = 0;
        while(inorder[i] != key){
            i++;
        }
        return i;
    }
}

 

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/132095169