-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
76 lines (71 loc) · 2.17 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class NumArray {
class TreeNode {
int start = 0;
int end = 0;
int sum = 0;
TreeNode left = null;
TreeNode right = null;
}
TreeNode root = null;
public NumArray(int[] nums) {
if (nums!=null&&nums.length>0) {
this.root=bulidTree(0, nums.length-1, nums);
}
}
public TreeNode bulidTree(int start,int end,int[] nums){
TreeNode node=new TreeNode();
node.start=start;
node.end=end;
if (start==end) {
node.sum=nums[start];
}else{
int mid=start+(end-start)/2;
node.left=bulidTree(start, mid, nums);
node.right=bulidTree(mid+1, end, nums);
node.sum=node.left.sum+node.right.sum;
}
return node;
}
void update(int i, int val) {
updateTree(this.root, i, val);
}
public void updateTree(TreeNode node,int index,int val){
if (node!=null) {
if (node.start==node.end) {
node.sum=val;
}else {
int mid=node.start+(node.end-node.start)/2;
if (index<=mid) {
updateTree(node.left, index, val);
}else{
updateTree(node.right, index, val);
}
node.sum=node.left.sum+node.right.sum;
}
}
}
public int sumRange(int i, int j) {
return queryTree(this.root, i, j);
}
public int queryTree(TreeNode node,int start,int end){
if (node==null) {
return 0;
}
if (node.start==start&&node.end==end) {
return node.sum;
}
int mid=node.start+(node.end-node.start)/2;
if (mid>=end) {
return queryTree(node.left, start, end);
}else if (mid<start) {
return queryTree(node.right, start, end);
}else{
return queryTree(node.left, start, mid)+queryTree(node.right, mid+1, end);
}
}
}
// Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);