简体   繁体   English

以无序和后置顺序创建树

[英]creating a tree out of inorder and postorder

Just for fun I am trying to create at tree out of inorder and postorder, and I know there are plenty of solutions online, but I am doing it by myself, and feel that I am very close, Here is my code: 只是为了好玩,我试图以无序和后序的形式在树上创建,而且我知道在线上有很多解决方案,但是我自己做,感觉很亲密,这是我的代码:

With the current input, I expect the tree 用当前输入,我期望树

     7
   5    10
4   6  8  11

yet what I get is 但我得到的是

    7
   5    10
4   6  11  11

public static TreeNode buildTreeHelper(int[] inorder, int[] postorder, int inOrderIndex, int end){
        if(end<0 || inOrderIndex<0){
            return null;
        }
        TreeNode root = new TreeNode(postorder[end]);
        int index = search(inorder,inOrderIndex,end,root.val);
        if(index!=-1){
            root.left = buildTreeHelper(inorder, postorder,inOrderIndex,index-1);
            root.right= buildTreeHelper(inorder, postorder,index+1,end-1);
        }
        return root;
    }

    public  static int search(int[]inorder, int start, int end, int target){
        for(int i=start; i<=end; i++){
            if(inorder[i]==target){
                return i;
            }
        }
        return -1;
    }    
    public static void main(String[] args){
        int[] inorder = {4, 5, 6, 7, 8, 10, 11};
        int[] postorder = {4, 6, 5, 8, 11, 10, 7};
        TreeNode ret = buildTreeHelper(inorder, postorder, 0, inorder.length-1);

    }

It looks to me like your inOrderIndex and end parameters can become inOrderIndex > end . 在我看来,您的inOrderIndexend参数可以变成inOrderIndex > end So I would check this logic. 因此,我将检查此逻辑。 I would debug to see at which point you are missing value 8 and using 10 instead. 我将进行调试,以查看在哪一点您缺少值8并改为使用10

The following is the steps suggested to solve the problem: 以下是建议解决该问题的步骤:

  1. The last element of post-order array is the root; 后置数组的最后一个元素是根。
  2. Find the index of the root in the in-order array; 在有序数组中找到根的索引;
  3. Calculate the length of left tree; 计算左树的长度;
  4. Calculate the start index and end index of in-order and post-order arrays; 计算顺序和后顺序数组的开始索引和结束索引;
  5. Recursively solve the left tree and right tree 递归求解左树和右树

In your solution of building the right tree, the start index of in-order array equals the start index of post-order array, which is incorrect. 在构建正确的树的解决方案中,顺序数组的起始索引等于后顺序数组的起始索引,这是不正确的。 For example, in the first iteration, the root is 7, the index of in-order is 3, the start index of in-order array for the right tree is 4 where stores value 8, but the start index of post-order is 3 where stores value 8. So you need to have separate indices for in-order and post-order. 例如,在第一个迭代中,根为7,顺序索引为3,右树的顺序数组的起始索引为4,其中存储值8,而后顺序的起始索引为3存储值8的位置。因此,您需要对有序和后序有单独的索引。

The solution would look like 解决方案看起来像

public static TreeNode buildTreeHelper(int[] inorder, int[] postorder,
    int inOrderStart, int inOrderEnd, int postOrderStart, int postOrderEnd) {
        if (inOrderEnd < 0 || inOrderStart < 0 || postOrderEnd < 0
                || postOrderStart < 0) {
            return null;
        }

        TreeNode root = new TreeNode(postorder[postOrderEnd]);

        // special case: when the node is a leaf
        if (inOrderStart == inOrderEnd) {
            return root;
        }

        int index = search(inorder, inOrderStart, inOrderEnd, root.val);
        if (index != -1) {

            //get the length of the left tree
            int leftTreeLen = index - inOrderStart; 

            root.left = buildTreeHelper(inorder, postorder, inOrderStart,
                    index - 1, postOrderStart, postOrderStart + leftTreeLen - 1);
            root.right = buildTreeHelper(inorder, postorder, index + 1,
                    inOrderEnd, postOrderStart + leftTreeLen, postOrderEnd - 1);
        }
        return root;
}

public static int search(int[] inorder, int start, int end, int target) {
        for (int i = start; i <= end; i++) {
            if (inorder[i] == target) {
                return i;
            }
        }
        return -1;
}

public static void main(String[] args) {
    int[] inorder = { 4, 5, 6, 7, 8, 10, 11 };
    int[] postorder = { 4, 6, 5, 8, 11, 10, 7 };
    TreeNode ret = buildTreeHelper(inorder, postorder, 0,
            inorder.length - 1, 0, postorder.length - 1);

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM