【Construct Binary Tree from Inorder and Postorder Traversal】cpp

题目:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

代码:

复制代码
/**
 * 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 {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if ( inorder.size()==0 || postorder.size()==0 ) return NULL;
        return Solution::buildTreeIP(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
    }
    static TreeNode* buildTreeIP(
        vector<int>& inorder, 
        int bI, int eI, 
        vector<int>& postorder, 
        int bP, int eP )
    {
        if ( bI > eI ) return NULL;
        TreeNode *root = new TreeNode(postorder[eP]);
        int rootPosInorder = bI;
        for ( int i = bI; i <= eI; ++i )
        {
            if ( inorder[i]==root->val ) { rootPosInorder=i; break; }
        }
        int leftSize = rootPosInorder - bI;
        int rightSize = eI - rootPosInorder;
        root->left = Solution::buildTreeIP(inorder, bI, rootPosInorder-1, postorder, bP, bP+leftSize-1);
        root->right = Solution::buildTreeIP(inorder, rootPosInorder+1, eI, postorder, eP-rightSize, eP-1);
        return root;
    }
};
复制代码

tips:

思路跟Preorder & Inorder一样。

这里要注意:

1. 算左子树和右子树长度时,要在inorder里面算

2. 左子树和右子树长度可能一样,也可能不一样;因此在计算root->left和root->right的时候,要注意如何切vector下标(之前一直当成左右树长度一样,debug了一段时间才AC)

==============================================

第二次过这道题,沿用了之前construct binary tree的思路,代码一次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 {
public:
        TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
        {
            return Solution::build(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
        }
        TreeNode* build(
            vector<int>& inorder, int bi, int ei,
            vector<int>& postorder, int bp, int ep)
        {
            if ( bi>ei || bp>ep) return NULL;
            TreeNode* root = new TreeNode(postorder[ep]);
            int right_range = ei - Solution::findPos(inorder, bi, ei, postorder[ep]);
            int left_range = ei - bi - right_range;
            root->left = Solution::build(inorder, bi, ei-right_range-1, postorder, bp, ep-right_range-1);
            root->right = Solution::build(inorder, bi+left_range+1 , ei, postorder, bp+left_range, ep-1);
            return root;
        }
        int findPos(vector<int>& order, int begin, int end, int val)
        {
            for ( int i=begin; i<=end; ++i ) if (order[i]==val) return i;
        }
};
复制代码

 

posted on   承续缘  阅读(153)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示