lintcode-线段树的构造-201

/**
 * Definition of SegmentTreeNode:
 * class SegmentTreeNode {
 * public:
 *     int start, end;
 *     SegmentTreeNode *left, *right;
 *     SegmentTreeNode(int start, int end) {
 *         this->start = start, this->end = end;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
public:
   
    SegmentTreeNode * build(int start, int end) {
        if(start>end)
            return nullptr;
        
        SegmentTreeNode *root= new SegmentTreeNode(start,end);
        if(start!=end){
            int mid=start+(end-start)/2;
            root->left=build(start,mid);
            root->right=build(mid+1,end);
        }
        return root;
    }
};

你可能感兴趣的:(Lintcode)