Java Simple and easy using Segment Tree solution, clean code with comments
PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\n\n\n\nclass NumArray {\n    SegmentTree segmentTree;\n    int n;\n\n    public NumArray(int[] nums) {\n        n = 
**PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n\n\n\n```\nclass NumArray {\n    SegmentTree segmentTree;\n    int n;\n\n    public NumArray(int[] nums) {\n        n = nums.length;\n        segmentTree = new SegmentTree(nums);\n    }\n    \n    public void update(int index, int val) {\n        segmentTree.update(0, 0, n - 1, index, val);\n    }\n    \n    public int sumRange(int left, int right) {\n        return segmentTree.getSum(0, 0, n - 1, left, right);\n    }\n}\n\nclass SegmentTree{\n    int[] tree;\n    int n;\n    int[] nums;\n    \n    \n    public SegmentTree(int[] nums){\n        this.nums = nums;\n        n = nums.length;\n        \n\t\t//reason for 4 * n\n\t\t//https://leetcode.com/problems/range-sum-query-mutable/discuss/1247355/Java-Simple-and-easy-using-Segment-Tree-solution-clean-code-with-comments/980720\n        tree = new int[4 * n];\n        \n        buildTree(0, 0, n - 1);\n    }\n    \n    private void buildTree(int treeIndex, int l, int r){\n        if(l == r){\n            tree[treeIndex] = nums[l];\n            return;\n        }\n        \n        int mid = l + ((r - l) / 2);\n        \n        //left subtree\n        int leftChildIndex  = 2 * treeIndex + 1;\n        buildTree(leftChildIndex, l, mid);\n        \n        //right subtree\n        int rightChildIndex = 2 * treeIndex + 2;\n        buildTree(rightChildIndex, mid + 1, r);\n        \n        //save the sum of both child\n        tree[treeIndex] = tree[leftChildIndex] + tree[rightChildIndex];\n    }\n    \n    \n    public int getSum(int treeIndex, int l, int r, int i, int j){\n        if(l > j || r < i) return 0;\n        \n        if(l >= i && r <= j) return tree[treeIndex];\n        \n        int mid = l + (r - l) / 2;\n        \n        int leftChildIndex = 2 * treeIndex + 1;\n        int rightChildIndex = 2 * treeIndex + 2;\n        \n        if(j <= mid){\n            \n            //As required range will be first half (left subtree)\n            //just check the first half \n            return getSum(leftChildIndex, l, mid, i, j);\n        }else if(i > mid){\n            \n            //As required range will be 2nd half (right subtree)\n            //just check the 2nd half only\n            return getSum(rightChildIndex, mid + 1, r, i, j);\n        }\n        \n        return getSum(leftChildIndex, l, mid, i, mid) + getSum(rightChildIndex, mid + 1, r, mid + 1, j);\n    }\n    \n    public void update(int treeIndex, int l, int r, int index, int val){\n        if(l == r){\n            tree[treeIndex] = val;\n            return;\n        }\n        \n        int mid = l + (r - l) / 2;\n        \n        int leftChildIndex  = 2 * treeIndex + 1;\n        int rightChildIndex  = 2 * treeIndex + 2;\n        \n        if(index <= mid){\n            update(leftChildIndex, l, mid, index, val);\n        }else if(index > mid){\n            update(rightChildIndex, mid + 1, r, index, val);\n        }\n        \n        \n        //save the sum of both child\n        tree[treeIndex] = tree[leftChildIndex] + tree[rightChildIndex];\n    }\n    \n    \n    \n}\n```
['Tree', 'Java']