从前序与中序遍历序列构造二叉树——从后序与中序序列构造二叉树

本文详细介绍了如何使用前序与中序遍历序列以及后序与中序遍历序列来构造二叉树。首先,通过前序遍历序列获取根节点并确定左右子树的边界,在中序遍历序列中找到根节点的位置,然后递归构造左右子树。其次,利用后序遍历序列,遵循先左右子树后根节点的顺序,同样结合中序遍历序列构建二叉树。代码实现中展示了关键的递归函数及其辅助方法。

目录

一.从前序与中序遍历序列构造二叉树

题目:

分析:

代码:

二.从后序与中序遍历序列构造二叉树

题目:

分析:

代码:


一.从前序与中序遍历序列构造二叉树

题目:

 

分析:

1.从前序遍历序列可以得到:二叉树从根结点到左子树再到右子树的遍历顺序。

2.从中序遍历序列可以得到:二叉树当前结点的左右子树分别为。

3.遍历前序遍历序列,用中序遍历序列的范围表示对应的左右子树,创建二叉树,创建顺序为先左子树后右子树。

代码:

  public int preIndex = 0;
    public TreeNode createTreeByPandI(int[] preorder,int[] inorder,int inbegin,int inend) {
        if (inbegin > inend) {
            return null;
        }
        //创建根结点
        TreeNode root = new TreeNode(preorder[preIndex]);
        //找到根结点在中序遍历序列中的坐标
        int rootIndex = findIndexOfI(inorder,inbegin,inend,preorder[preIndex]);
        //排除找不到根结点的可能
        if (rootIndex == -1) return null;
        preIndex++;
        root.left = createTreeByPandI(preorder,inorder,inbegin,rootIndex - 1);
        root.right = createTreeByPandI(preorder,inorder,rootIndex + 1,inend);
        return root;
    }

    //找到根结点在中序遍历序列中的坐标
    public int findIndexOfI(int[] inorder,int inbegin,int inend,int key) {
        for (int i = inbegin;i <= inend;i ++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
    
    //带入两个序列创建二叉树
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || inorder == null) return null;
        TreeNode root = createTreeByPandI(preorder,inorder,0,inorder.length - 1);
        return root;
    }

二.从后序与中序遍历序列构造二叉树

题目:

 

分析:

1.从后序遍历序列可以得到:二叉树从左子树到右子树再到根结点的遍历顺序。

2.从中序遍历序列可以得到:二叉树当前结点的左右子树分别为。

3.遍历后序遍历序列,用中序遍历序列的范围表示对应的左右子树,创建二叉树,创建顺序为先右子树,后左子树。

代码:

   public int postIndex = 0;
    public TreeNode createTree(int[] inorder,int[] postorder,int inbegin,int inend) {
        if (inend < inbegin) return null;
        //创建结点
        TreeNode root = new TreeNode(postorder[postIndex]);
        //找到该结点在中序遍历序列当中的坐标
        int rootIndex = findIndexOfI(inorder,inbegin,inend,postorder[postIndex]);
        //排除找不到结点的可能
        if (rootIndex == -1) {
            return null;
        }
        postIndex--;
        root.right = createTree(inorder,postorder,rootIndex + 1,inend);
        root.left = createTree(inorder,postorder,inbegin,rootIndex - 1);
        return root;
    }
    //根据该结点的值找到其在中序遍历序列当中的坐标
    public int findIndexOfI(int[] inorder,int inbegin,int inend,int key) {
        for (int i = inbegin;i <= inend;i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }

    //带入两个序列创建二叉树
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (inorder == null || postorder == null) return null;
        postIndex = postorder.length - 1;
        TreeNode root = createTree(inorder,postorder,0,inorder.length - 1);
        return root;
    }

从前序遍历序列构造二叉树可以使用迭代和递归两种方法。 ### 迭代方法 迭代方法利用栈来模拟递归调用的过程。以下是实现代码: ```cpp #include <vector> #include <stack> // 定义二叉树节点结构 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class Solution { public: TreeNode* buildTree(std::vector<int>& preorder, std::vector<int>& inorder) { if (!preorder.size()) { return nullptr; } TreeNode* root = new TreeNode(preorder[0]); std::stack<TreeNode*> stk; stk.push(root); int inorderIndex = 0; for (int i = 1; i < preorder.size(); ++i) { int preorderVal = preorder[i]; TreeNode* node = stk.top(); if (node->val != inorder[inorderIndex]) { node->left = new TreeNode(preorderVal); stk.push(node->left); } else { while (!stk.empty() && stk.top()->val == inorder[inorderIndex]) { node = stk.top(); stk.pop(); ++inorderIndex; } node->right = new TreeNode(preorderVal); stk.push(node->right); } } return root; } }; ``` 迭代方法的思路是:首先创建根节点并将其压入栈中。然后遍历前序遍历数组,对于每个元素,判断它是栈顶节点的左子节点还是某个节点的右子节点。如果它不是当前中序遍历指针所指元素,则作为栈顶节点的左子节点;否则,通过弹出栈中的节点,找到合适的父节点来插入该元素作为右子节点 [^2]。 ### 递归方法 递归方法基于前序遍历和中序遍历的特性。前序遍历的第一个元素是根节点,在中序遍历中找到该根节点的位置,其左边的元素构成左子树的中序遍历,右边的元素构成右子树的中序遍历。根据左右子树的元素数量,可以在前序遍历中划分出左右子树的前序遍历。然后递归地构建左右子树。以下是实现代码: ```python # 定义二叉树节点类 class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): if not preorder or not inorder: return None # 前序遍历的第一个元素是根节点 root_val = preorder[0] root = TreeNode(root_val) # 在中序遍历中找到根节点的位置 inorder_index = inorder.index(root_val) # 递归构建左子树 root.left = buildTree(preorder[1:inorder_index + 1], inorder[:inorder_index]) # 递归构建右子树 root.right = buildTree(preorder[inorder_index + 1:], inorder[inorder_index + 1:]) return root ``` 递归方法的核心是利用前序遍历和中序遍历的特性,不断地划分左右子树的范围,然后递归构建左右子树。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爆裂突破手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值