LeetCode: 889. Construct Binary Tree from Preorder and Postorder Traversal

本文介绍LeetCode上第889题Construct Binary Tree from Preorder and Postorder Traversal的解题思路及实现代码。该题要求从给定的先序遍历和后序遍历序列中构造任意一棵匹配的二叉树。通过递归方法,利用先序遍历的第一个元素确定根节点,并找到左子树的根节点,从而划分左右子树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode: 889. Construct Binary Tree from Preorder and Postorder Traversal

题目描述

Return any binary tree that matches the given preorder and postorder traversals.
Values in the traversals pre and post are distinct positive integers.

Example 1:

Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]

Note:

1 <= pre.length == post.length <= 30
pre[] and post[] are both permutations of 1, 2, ..., pre.length.
It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.

解题思路

一般遇到二叉树的题都用递归求解。很明显,先序和后序复原的二叉树是不唯一,我们只需要根据某种规律恢复可能的一种二叉树。如,可以根据先序遍历找到第一个子树的头结点,作为左子树。

AC 代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    TreeNode* constructFromPrePost(vector<int>& pre, int prebeg, int preend, 
                                  vector<int>& post, int postbeg, int postend)
    {
        if(prebeg >= preend) return nullptr;

        TreeNode* curNode = new TreeNode(pre[prebeg]);

        // 左子树
        if(prebeg + 1 == preend) return curNode;
        size_t leftRootIdx = postbeg;
        while(post[leftRootIdx] != pre[prebeg+1] && leftRootIdx != postend-1) ++leftRootIdx;
       // cout << pre[prebeg+1] << " " << pre[prebeg+leftRootIdx-postbeg+1] << endl;
        curNode->left = constructFromPrePost(pre, prebeg+1, prebeg+leftRootIdx-postbeg+2, 
                                             post, postbeg, leftRootIdx+1);

        // 右子树
        if(leftRootIdx + 1 == postend-1) return curNode;
        curNode->right = constructFromPrePost(pre, prebeg+leftRootIdx-postbeg+2,preend, 
                                            post, leftRootIdx+1, postend-1);

        return curNode;
    }
public:
    TreeNode* constructFromPrePost(vector<int>& pre, vector<int>& post) {
        return constructFromPrePost(pre, 0, pre.size(), post, 0, post.size());
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值