剑指offer---重建二叉树

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;
    }
};

 

posted @ 2017-08-02 11:23  双马尾是老公的方向盘  阅读(160)  评论(0编辑  收藏  举报