201. Segment Tree Build

最后更新

二刷
08-Jan-2017

一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方。

其实重要的3个东西题目已经提供了:

  1. left < right return null
  2. left == right return root
  3. (left, mid), (mid+1, right)
public class Solution {

     public SegmentTreeNode build(int start, int end) {
        if (start > end) return null;
        SegmentTreeNode tempRoot = new SegmentTreeNode(start, end);
        if (start == end) return tempRoot;
        int mid = start + (end - start) / 2;
        tempRoot.left = build(start, mid);
        tempRoot.right = build(mid + 1, end);
        return tempRoot;
     }

}
原文地址:https://www.cnblogs.com/reboot329/p/6263871.html