码迷,mamicode.com
首页 > 其他好文 > 详细

剑指offer---重建二叉树

时间:2017-08-02 11:50:29      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:node   turn   --   code   pre   treenode   logs   public   div   

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---重建二叉树

标签:node   turn   --   code   pre   treenode   logs   public   div   

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

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