Find the post-order sequence from the pre-order and middle-order sequence of the binary tree

When encountering this problem, the first time I want to restore the binary tree, but this is always a bypass.

Record https://blog.csdn.net/qq_29375837/article/details/81160362

Recursive ideas seen

private static void coverTree(int[] pre, int[] order, int[] post, int i) {
   for (int j = 0; j < order.length; j++) {
      if(order[j]==pre[0]) {
         post[i]=order[j];//赋值

//左子树
coverTree(Arrays.copyOfRange(pre,1,j+1),Arrays.copyOfRange(order,0,j),post,i-j-1);
//右子树
coverTree(Arrays.copyOfRange(pre,j+1,pre.length),Arrays.copyOfRange(order, j+1,order.length),post,i-1);

            }
        }
}

 

Guess you like

Origin blog.csdn.net/qhlpptdyfn/article/details/89288083