首页 > 其他 > 详细

剑指offer---重建二叉树

时间:2017-08-02 11:40:16      阅读:139      评论:0      收藏:0      [点我收藏+]
class Solution 
{
public:
    TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin)
    {
        int in_size = vin.size();
        if (in_size == 0)    return NULL;
        vector<int> XianXu_left;
        vector<int> XianXu_right;
        vector<int> ZhongXu_left;
        vector<int> ZhongXU_right;

        TreeNode* Node = new TreeNode(pre[0]);

        int weizhi = 0;
        for (int i = 0; i < in_size; ++i)
        {
        
            if (vin[i] == pre[0])
            {
                break; 
            }
            ++weizhi;
        }

        for (int i = 0; i < weizhi; ++i)
        {
            ZhongXu_left.push_back(vin[i]);
        }

        for (int i = (weizhi + 1); i < in_size; ++i)
        {
            ZhongXU_right.push_back(vin[i]);
        }

        for (int i = 1; i < (ZhongXu_left.size() + 1); ++i)
        {
            XianXu_left.push_back(pre[i]);
        }

        for (int i = (ZhongXu_left.size() + 1); i < pre.size(); ++i)
        {
            XianXu_right.push_back(pre[i]);
        }


        Node->left = reConstructBinaryTree(XianXu_left, ZhongXu_left);
        Node ->right= reConstructBinaryTree(XianXu_right, ZhongXU_right);
        return Node;
    }
};

 

剑指offer---重建二叉树

原文:http://www.cnblogs.com/159269lzm/p/7272955.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!