首页 > 试题广场 >

重建二叉树

[编程题]重建二叉树
  • 热度指数:1319097 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定节点数为 n 的二叉树的前序遍历和中序遍历结果,请重建出该二叉树并返回它的头结点。
例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建出如下图所示。


提示:
1.vin.length == pre.length
2.pre 和 vin 均无重复元素
3.vin出现的元素均出现在 pre里
4.只需要返回根结点,系统会自动输出整颗树做答案对比
数据范围:,节点的值
要求:空间复杂度 ,时间复杂度
示例1

输入

[1,2,4,7,3,5,6,8],[4,7,2,1,5,3,8,6]

输出

{1,2,3,4,#,5,6,#,7,#,#,8}

说明

返回根节点,系统会输出整颗二叉树对比结果,重建结果如题面图示    
示例2

输入

[1],[1]

输出

{1}
示例3

输入

[1,2,3,4,5,6,7],[3,2,4,1,6,5,7]

输出

{1,2,5,3,4,6,7}

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
推荐
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
    	TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
    	return root;
    }
    //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
    private TreeNode reConstructBinaryTree(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]);
    	
    	for(int i=startIn;i<=endIn;i++)
    		if(in[i]==pre[startPre]){
    			root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
    			root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                      break;
    		}
    			
    	return root;
    }
}

编辑于 2018-03-21 14:58:42 回复(255)
int searchArr(int *Arr, int ArrLen, int p) {
    int i;
    for(i=0;i<ArrLen;i++) {
        if(p==Arr[i])
            return i;
    }
    return -1;
}
struct TreeNode* reConstructBinaryTree(int* preOrder, int preOrderLen, int* vinOrder, int vinOrderLen ) {
    struct TreeNode* res;
    int leftTreeLen, rightTreeLen;
    if(!preOrderLen)
        return NULL;

    res = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    res->val = preOrder[0];
    
    leftTreeLen = searchArr(vinOrder, vinOrderLen, preOrder[0]);
    rightTreeLen = preOrderLen-1-leftTreeLen;
    res->left = reConstructBinaryTree(preOrder+1, leftTreeLen, vinOrder, leftTreeLen);
    res->right = reConstructBinaryTree(preOrder+1+leftTreeLen, rightTreeLen, vinOrder+leftTreeLen+1, rightTreeLen);
    return res;
}

编辑于 2024-03-17 13:57:20 回复(0)
可以看一下代码随想录里相关题目讲解,讲的很清楚
发表于 2024-01-11 10:30:07 回复(0)
/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param preOrder int整型一维数组 
 * @param preOrderLen int preOrder数组长度
 * @param vinOrder int整型一维数组 
 * @param vinOrderLen int vinOrder数组长度
 * @return TreeNode类
 */
#include <stdlib.h>
struct TreeNode* reConstructBinaryTree(int* preOrder, int preOrderLen, int* vinOrder, int vinOrderLen ) {
    // write code here
    if (preOrderLen == 0) {
        return NULL;
    }
    int rootValue = *preOrder;
    int index = 0;
    while (vinOrder[index] != rootValue) {
        index ++;
    }
    struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = rootValue;
    root->left = reConstructBinaryTree(preOrder + 1, index, vinOrder, index);
    root->right = reConstructBinaryTree(preOrder + 1 + index, preOrderLen - index - 1, vinOrder + index + 1, vinOrderLen - index - 1);
    return root;
}

发表于 2024-01-03 22:49:03 回复(0)
struct TreeNode* reConstructBinaryTree(int* preOrder, int preOrderLen, int* vinOrder, int vinOrderLen )
{
    // write code here
    struct TreeNode*root=NULL;
    if(preOrderLen&&vinOrderLen)
    {
        root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
        root->val=*preOrder;
        root->left=NULL;
        root->right=NULL;
        struct TreeNode*left=NULL;
        struct TreeNode*right=NULL;
        int i=0;
        while(*(vinOrder+i)!=*preOrder)
        {
            i++;
        }
        i++;
        left=reConstructBinaryTree(preOrder+1,i-1,vinOrder,i-1);
        right=reConstructBinaryTree(preOrder+i,vinOrderLen-i,vinOrder+i,vinOrderLen-i);
        root->left=left;
        root->right=right;
    }

    return root;
}
发表于 2023-06-30 19:08:09 回复(0)
请问大伙,为啥我这最后一个用例能输出,而且在执行结果的可显示范围里和预期输出一致,可是却报段错误?
struct TreeNode* reConstructBinaryTree(int* preOrder, int preOrderLen, int* vinOrder, int vinOrderLen ) {
    // write code here
    if (preOrderLen == 0) {
        return NULL;
    }

    int precur = 0, vincur = 0, top = 0;
    bool flag = false;
    struct TreeNode* stack[2000];

    struct TreeNode* root, * now;
    root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->val = preOrder[precur++];

    stack[0] = root;

    while (precur < preOrderLen) {
        while (stack[top]->val == vinOrder[vincur]) {
            vincur++;
            now = stack[top--];
            flag = true;
        }
        struct TreeNode* temp;
        temp = (struct TreeNode*)malloc(sizeof(struct TreeNode));
        temp->val = preOrder[precur++];

        if (flag) {
            now->right = temp;
            flag = false;
        } else {
            stack[top]->left = temp;
        }

        top++;
        stack[top] = temp;
    }

    return root;
}

发表于 2023-06-30 07:51:58 回复(0)
 void _reConstructBinaryTree(struct TreeNode** root,int* pre, int preLen, int* idx,int* vin, int l,int r,int dir) 
 {
    if(l>r)
        return;
    int i = l; struct TreeNode* node;
    for(i=l;i<=r;i++)
    {
        if(pre[*idx] == vin[i])
        {
           (*idx) ++;
           node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
           if(node == NULL)
                return;
            node->val = vin[i];
            node->left = NULL;
            node->right = NULL;
            if(*root == NULL)
                *root = node;
            else {
                if(dir == 0)
                    (*root)->left = node;
                else
                    (*root)->right = node;
            }
            break;       
        }
    }
    _reConstructBinaryTree(&node,pre,preLen,idx,vin,l,i-1,0);
    _reConstructBinaryTree(&node,pre,preLen,idx,vin,i+1,r,1);
 }
struct TreeNode* reConstructBinaryTree(int* pre, int preLen, int* vin, int vinLen ) 
{
    // write code here
    struct TreeNode* root = NULL;
    int idx = 0;
    _reConstructBinaryTree(&root,pre,preLen,&idx,vin,0,vinLen-1,-1);
    return root;
}

发表于 2023-05-28 22:55:59 回复(0)