(int[] nums)
| 10 | |
| 11 | // O(n) |
| 12 | private void buildTree(int[] nums) { |
| 13 | // we build the tree from bottom up |
| 14 | for (int i = n, j = 0; i < 2 * this.n; i++, j++) { |
| 15 | this.tree[i] = nums[j]; |
| 16 | } |
| 17 | for (int i = this.n-1; i > 0; i--) { |
| 18 | this.tree[i] = this.tree[2 * i] + this.tree[2 * i + 1]; // parent value is sum of children values |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // O(logn) |
| 23 | public void update(int index, int val) { |