-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbstSum.java
More file actions
49 lines (39 loc) · 1.05 KB
/
bstSum.java
File metadata and controls
49 lines (39 loc) · 1.05 KB
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
public class bstSum {
static class Node {
Node left;
Node right;
int data;
public Node(int data) {
this.data = data;
}
public int sum() {
int sum = data;
if (left != null) {
sum += left.sum();
}
if (right != null) {
sum += right.sum();
}
return sum;
}
}
public static void main(String[] args) {
// Our example tree looks like this:
// 2
// / \
// 3 4
// / \
// 5 6
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
node2.left = node3;
node2.right = node4;
node3.left = node5;
node3.right = node6;
System.out.println("Sum of all values of this tree is (should print 20):");
System.out.println(node2.sum());
}
}