码迷,mamicode.com
首页 > 编程语言 > 详细

根据后序遍历和中序遍历的数组构建二叉树

时间:2015-07-24 20:34:30      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

我的代码是:

TreeNode* buildTree (vector<int> &inorder, vector<int> &postorder)
{
    if (inorder.empty ()) {
        return nullptr;
    }
    
    unordered_map<int, INTVECIT> inItDic;
    unordered_map<int, INTVECIT> postItDic;
    
    for (auto it = inorder.begin(); it != inorder.end(); ++it){
        inItDic[*it] = it;
    }
    
    for (auto it = postorder.begin(); it != postorder.end(); ++it){
        postItDic[*it] = it;
    }
    
    auto rootVal      = postorder.back ();
    auto root         = new TreeNode (rootVal);
    auto rootIt = inItDic[rootVal];
    
    function<void(TreeNode*, INTVECIT, INTVECIT)> build;
    build = [&] (TreeNode *subRoot, INTVECIT begin, INTVECIT end)
    {
        if (begin == end) {
            return;
        }
        
        set<INTVECIT> weights;
        for (auto it = begin; it != end; ++it) {
            weights.insert (postItDic[*it]);
        }
        auto newSubRootVal = **weights.crbegin ();
        auto newSubRoot = new TreeNode (newSubRootVal);
        
        if (newSubRootVal < subRoot->val) {
            subRoot->left = newSubRoot;
        }
        else {
            subRoot->right = newSubRoot;
        }
        
        auto newSubRootIt = inItDic[newSubRootVal];
        build (newSubRoot, begin, newSubRootIt);
        build (newSubRoot, newSubRootIt + 1, end);
    };
    
    build (root, inorder.begin(), rootIt);
    build (root, rootIt + 1, inorder.end());
    
    return root;
}

 运行时间多久? 2.67s 我可是自认挺好了, 毕竟我刚写好代码的时候可是运行了 20s+ 

可是 leetcode 并不认同,我就崩溃了,卧槽这都超时?两秒半也超时? 你特么在逗我!

直到在知乎上,一位好心前辈给了我一个解答,还就在我的代码基础上改的:

TreeNode* buildTree (vector<int> &inorder, vector<int> &postorder)
{
    using IndexType = vector<int>::size_type;
    if (inorder.empty () || postorder.empty() || inorder.size() != postorder.size()) {
        return nullptr;
    }
    
    unordered_map<int, int> inIndexDic;
    
    for (auto i = 0; i < inorder.size(); ++i) {
        inIndexDic[inorder[i]] = i;
    }
    
    function<TreeNode*(IndexType, IndexType, IndexType, IndexType)> build;
    build = [&] (IndexType inBegin, IndexType inEnd, IndexType postBegin, IndexType postEnd) -> TreeNode*
    {
        if (inBegin == inEnd) {
            return nullptr;
        }
        
        IndexType newRootIndex = inIndexDic[postorder[postEnd - 1]];
        IndexType leftSize = newRootIndex - inBegin;
        IndexType rightSize = inEnd - newRootIndex - 1;
        
        auto newRoot = new TreeNode(inorder[newRootIndex]);
        newRoot->left = build(inBegin, newRootIndex, postBegin, postBegin + leftSize);
        newRoot->right = build(newRootIndex + 1, inEnd, postBegin + leftSize, postBegin + leftSize + rightSize);
        return newRoot;
    };
    
    return build(0, inorder.size(), 0, postorder.size());
}

用了多少时间? 

0.002547s

我还是太年轻。

根据后序遍历和中序遍历的数组构建二叉树

标签:

原文地址:http://www.cnblogs.com/wuOverflow/p/4674283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!