-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTrees.java
More file actions
42 lines (36 loc) · 955 Bytes
/
TestTrees.java
File metadata and controls
42 lines (36 loc) · 955 Bytes
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
package Trees;
public class TestTrees {
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(sumValues(node2));
}
static int sumValues(Node root) {
if (root == null) {
return 0;
}
return root.data + sumValues(root.left) + sumValues(root.right);
}
}
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
}
}