lintcode-Segmemt Tree Build II-439

来源:互联网 发布:局域网语音软件 编辑:程序博客网 时间:2024/05/22 15:40
/** * Definition of SegmentTreeNode: * class SegmentTreeNode { * public: *     int start, end, max; *     SegmentTreeNode *left, *right; *     SegmentTreeNode(int start, int end, int max) { *         this->start = start; *         this->end = end; *         this->max = max; *         this->left = this->right = NULL; *     } * } */class Solution {public:        SegmentTreeNode * build(int l,int r,vector<int>& A){        if(l>r)            return NULL;                     SegmentTreeNode *root=new SegmentTreeNode(l,r);        if(l!=r){            int mid=l+(r-l)/2;            root->left=build(l,mid,A);            root->right=build(mid+1,r,A);            root->max=max(root->left->max,root->right->max);        }else{            root->max=A[l];            }        return root;    }        SegmentTreeNode * build(vector<int>& A) {        if(A.empty())            return NULL;        return build(0,A.size()-1,A);        }};

0 0
原创粉丝点击