根据二叉树中序和后序(先序)遍历结果 重建二叉树

题目描述
根据中序遍历和后序遍历构造二叉树
From the post-order array, we know that last element is the root. We can find the root in in-order array. Then we can identify the left and right sub-trees of the root from in-order array.

Using the length of left sub-tree, we can identify left and right sub-trees in post-order array. Recursively, we can build up the tree

public TreeNode buildTree(int[] inorder, int[] postorder) {
    int inStart = 0;
    int inEnd = inorder.length - 1;
    int postStart = 0;
    int postEnd = postorder.length - 1;

    return buildTree(inorder, inStart, inEnd, postorder, postStart, postEnd);
}

public TreeNode buildTree(int[] inorder, int inStart, int inEnd,
        int[] postorder, int postStart, int postEnd) {
    if (inStart > inEnd || postStart > postEnd)
        return null;

    int rootValue = postorder[postEnd];
    TreeNode root = new TreeNode(rootValue);

    int k = 0;
    for (int i = 0; i < inorder.length; i++) {
        if (inorder[i] == rootValue) {
            k = i;
            break;
        }
    }

    root.left = buildTree(inorder, inStart, k - 1, postorder, postStart,
            postStart + k - (inStart + 1));
    // Becuase k is not the length, it it need to -(inStart+1) to get the length
    root.right = buildTree(inorder, k + 1, inEnd, postorder, postStart + k- inStart, postEnd - 1);
    // postStart+k-inStart = postStart+k-(inStart+1) +1

    return root;
}

/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }

根据先序遍历和中序遍历构造二叉树

 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root = reconstructBTNode(pre,0,pre.length-1, in, 0, in.length-1);
        return root;
    }
      public static TreeNode reconstructBTNode(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn){
        if(startPre > endPre || startIn > endIn)  return null;
        //重建根结点
        TreeNode root = new TreeNode(pre[startPre]);
        //根据root.val 在中序序列中确定根结点的位置下标i;
        for(int i = startIn ; i < in.length; i++){
            if(in[i] == root.val){
                //左子树序列
                root.left = reconstructBTNode(pre, startPre+1, startPre+i-startIn, in ,startIn, i-1);
                //右子树序列
                root.right = reconstructBTNode(pre,startPre+i-startIn+1, endPre, in , i+1, endIn);
            }
        }
        return root;
    } 
}
<think>嗯,用户的问题是根据二叉树序遍历来计算后序遍历。我需要回忆一下相关的二叉树遍历知识,然后思考如何通过这两种遍历结果重建二叉树,进而得到后序遍历。 首序遍历的顺是根节点、左子树、右子树,而中序遍历是左子树、根节点、右子树。所以,序遍历的第一个元素肯定是整个树的根节点。然后在中序遍历中找到这个根节点,左边就是左子树的中序遍历结果,右边是右子树的中序遍历结果。接下来,可以通过递归的方法分别处理左子树右子树。 举个例子,假设是[3,9,20,15,7],中是[9,3,15,20,7]。的第一个元素3是根节点。在中中,3左边的9是左子树的中,右边的15,20,7是右子树的中。然后,左子树的应该是中根之后的部分,长度左子树的长度相同,也就是9。右子树的则是剩下的20,15,7。这样递归下去,就能重建整个二叉树的结构。重建之后再进行后序遍历,就能得到结果了。 不过要注意边界条件,比如当左子树或右子树为空的时候,递归应该停止。另外,如何高效地在中序遍历中找到根节点的位置?可能需要用哈希表来存储中序遍历的值索引,这样查找起来更快,时间复杂度更低。 用户可能还需要具体的实现步骤或者代码示例。根据引用的内容,比如引用[2]提到了递归非递归的实现方法,但这里可能需要用递归来重建二叉树,然后再进行后序遍历。所以代码部分应该包括构建二叉树后续遍历两个部分。 还需要考虑输入的有效性,比如序遍历的长度是否一致,以及是否确实对应同一棵树。如果输入不合法,比如无法构建二叉树,应该如何处理这些错误情况。 另外,用户可能想知道时间复杂度空间复杂度。构建哈希表是O(n)的时间,递归重建二叉树每个节点访问一次,所以时间复杂度是O(n)。空间复杂度主要是递归栈的深度,最坏情况下是O(n),平均是O(log n)(平衡二叉树的情况下)。 最后,生成相关问题的时候,可以考虑其他遍历组合的可能性,或者如何优化算法,或者实际应用场景。</think>### 通过序遍历计算后序遍历的方法 通过二叉树序遍历序遍历推导后序遍历的核心步骤为: 1. **确定根节点**:序遍历的第一个元素即为根节点值 2. **划分左右子树**:在中序遍历中找到根节点,左侧为左子树的中列,右侧为右子树的中列 3. **递归构建子树**:根据左右子树的长度,分割列为左子树右子树 4. **生成后序遍历**:递归完成后序遍历的拼接顺为左子树后序 + 右子树后序 + 根节点 #### 算法实现(Python示例) ```python def build_tree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root_idx = inorder.index(root_val) left_in = inorder[:root_idx] right_in = inorder[root_idx+1:] left_pre = preorder[1:1+len(left_in)] right_pre = preorder[1+len(left_in):] root = {'val': root_val} root['left'] = build_tree(left_pre, left_in) root['right'] = build_tree(right_pre, right_in) return root def postorder(root): if not root: return [] return postorder(root['left']) + postorder(root['right']) + [root['val']] # 示例输入 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] tree = build_tree(preorder, inorder) print("后序遍历结果:", postorder(tree)) # 输出: [9,15,7,20,3] ``` #### 关键说明 1. **时间复杂度**:$O(n^2)$(未使用哈希表优化),若用哈希表存储中索引可优化至$O(n)$[^2] 2. **边界条件**:当列不匹配时无法构建树(需额外校验) 3. **空间复杂度**:递归调用栈深度为树的高度,最坏情况下为$O(n)$[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值