剑指offer面试题7:重建二叉树

 题目描述:

输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。

假设输入的前序遍历和中序遍历的结果中都不含重复的数字。

示例 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
示例 2:

Input: preorder = [-1], inorder = [-1]
Output: [-1]

知识点:

  • 前序遍历列表:第一个元素永远是 【根节点 (root)】
  • 中序遍历列表:根节点 (root)【左边】的所有元素都在根节点的【左分支】,【右边】的所有元素都在根节点的【右分支】

算法思路:

  1. 通过【前序遍历列表】确定【根节点 (root)】
  2. 将【中序遍历列表】的节点分割成【左分支节点】和【右分支节点】
  3. 递归寻找【左分支节点】中的【根节点 (left child)】和 【右分支节点】中的【根节点 (right child)】
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
 
//1、建立根节点,前序遍历中第一个数字得到根节点值preorder[0]
//2、根据根节点来划分 前序数组 和 中序数组,找到 中序数组 中根节点的位置
//3、根据根节点的位置划分中序数组,中序数组根节点的左侧为左子树,右侧为右子树
//4、划分前序数组,根据中序数组的左右子树的大小
//5、递归处理左右子树

class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size()==0) return NULL;
        else
        {
            int rootVal = preorder[0];   //根节点为前序遍历的第一个元素
            TreeNode *root = new TreeNode(rootVal);
            //查找中序遍历中根节点所在位置
            vector<int>::iterator pos = find(inorder.begin(),inorder.end(),rootVal);
            
            vector<int> inLeft(inorder.begin(),pos);
            vector<int> inRight(pos+1,inorder.end());
            int inLeftSize = inLeft.size();
            vector<int> preLeft(preorder.begin()+1,preorder.begin()+inLeftSize+1);
            vector<int> preRight(preorder.begin()+inLeftSize+1,preorder.end());

            root->left=buildTree(preLeft,inLeft);
            root->right=buildTree(preRight,inRight);
            return root;
        }
        
    }
};
            //获取中序遍历中根节点的下标
            int index=0;
            for(;index<inorder.size();index++)
            {
                if(rootVal==inorder[index]) break;
            }

            //根据根节点的位置划分中序数组,中序数组根节点的左侧为左子树,右侧为右子树
            //划分前序数组,根据中序数组的左右子树的大小
            vector<int> preLeft;  
            preLeft.assign(preorder.begin()+1,preorder.begin()+1+index);
            vector<int> preRight; 
            preRight.assign(preorder.begin()+1+index,preorder.end());
            vector<int> inLeft;   
            inLeft.assign(inorder.begin(),inorder.begin()+index);
            vector<int> inRight;  
            inRight.assign(inorder.begin()+index+1,inorder.end());
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.size()==0 || inorder.size()==0) return NULL;

        static int k=0;
        int rootval = preorder[k++];
        TreeNode *root = new TreeNode(rootval);

        vector<int>::iterator pos = find(inorder.begin(),inorder.end(),rootval);
        if(pos != inorder.end())
        {
            vector<int> inorderLeft = {inorder.begin(),pos};
            vector<int> inorderRight = {pos+1,inorder.end()};

            root->left = buildTree(preorder,inorderLeft);
            root->right = buildTree(preorder,inorderRight);
        }

        return root;
    }
};

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值