4:重建二叉树

题目描述

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

1、基础知识

二叉树

参考:

深入学习二叉树(一) 二叉树基础:https://www.jianshu.com/p/bf73c8d50dc2

二叉树:https://blog.youkuaiyun.com/heyanxi0101/article/details/79593542

2、思路

从前序遍历找出root

确定root值在中序遍历中位置,然后分左右树

确定左右树之后,在按以上方法寻找下一个gen,在按左右分

3、原理图

4、代码实现

参考:https://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6

#-*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    # 返回构造的TreeNode跟节点
    def reConstructBinaryTree(self, pre, tin):
        if not pre or not tin:
            return None
        root = TreeNode(pre.pop(0)) # 得到前序遍历中跟节点
        index = tin.index(root.val) # 找出跟节点在中序遍历的位置

        root.left = self.reConstructBinaryTree(pre, tin[:index])
        root.right = self.reConstructBinaryTree(pre, tin[index + 1:])
        return root

另:代码未通过

class Solution:
    # 返回构造的TreeNode跟节点
    def reConstructBinaryTree(self, pre, tin):
        if len(pre) == 0:
            return None
        root = TreeNode(pre[0])
        index = tin.index(root.val)
        root.left = self.reConstructBinaryTree(pre[1:index+1], tin[:index])
        root.right = self.reConstructBinaryTree(pre[index+1:], tin[index+1:])
        return root

代码未通过:

#include<iostream>
#include<vector>
/*
作者:Monotone
链接:https://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6
来源:牛客网
次将左右两颗子树当成新的子树进行处理,中序的左右子树索引很好找,
前序的开始结束索引通过计算中序中左右子树的大小来计算,
然后递归求解,直到startPre>endPre||startIn>endIn说明子树整理完到。方
法每次返回左子树活右子树的根节点*/
using namespace std;

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x):val(x),left(NULL),right(NULL){
    }
};

struct TreeNode *reConstructBinaryTree(vector<int>pre, vector<int >in)
{
    if(pre.size() == NULL)
    return NULL;
    TreeNode *root = new TreeNode(pre[0]);// 辟一个存放 存储空间,返回一个指向该存储空间的地址(即指针)
    int i;
    for(i=0;i<in.size()&&in[i]!=pre[0];i++)
    vector<int>pre_letf,in_left,pre_right,in_right; 
    int pre_i = 1;
    for(int j=0;j<in.size();j++)
    {
        if(j<i)
        {
            in_left.push_back(in[j]);//vector(向量,可操作的动态数组)一个方法,在数组左后添加一个数据 
            pre_left.push_back(pre[pre_i]);//vetcor提高一系列操作数组的接口 
            pre_i++;
        }else if(j>i)
        {
            in_right.push_back(in[j]);
            pre_rifht.push_back(pre[pre_i]);
            pre_i++;
        }
        root->left=reConstructBinaryTree(pre_left,in_left);
        root->right= reConstructBinaryTree(pre_right,in_right);
        return root;
}

int main(){
    vector<int> pre={1,2,3,4,5,6,8};//创建一个Int型的动态数组 
    vector<int> in={4,7,2,1,5,3,8,6};
    TreeNode *root=reConstructBinaryTree(pre,in);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值