-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tree_SumOfLeafNodes.java
65 lines (54 loc) · 1.37 KB
/
Tree_SumOfLeafNodes.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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
// https://practice.geeksforgeeks.org/problems/sum-of-leaf-nodes/1
// https://www.geeksforgeeks.org/sum-leaf-nodes-binary-tree/
public class Tree_SumOfLeafNodes {
private static int getSumofLeafNodes(Node root) {
int sumOfLeafNodes = 0;
if (root == null)
return 0;
if (root.left == null && root.right == null)
return root.data;
sumOfLeafNodes += getSumofLeafNodes(root.left);
sumOfLeafNodes += getSumofLeafNodes(root.right);
return sumOfLeafNodes;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
Map<Integer, Node> m = new HashMap<>();
int n = sc.nextInt();
Node root = null;
while (n-- > 0) {
int n1 = sc.nextInt();
int n2 = sc.nextInt();
char lr = sc.next().charAt(0);
Node parent = m.get(n1);
if (parent == null) {
parent = new Node(n1);
m.put(n1, parent);
if (root == null)
root = parent;
}
Node child = new Node(n2);
if (lr == 'L')
parent.left = child;
else
parent.right = child;
m.put(n2, child);
}
System.out.println(getSumofLeafNodes(root));
}
sc.close();
}
private static class Node {
private int data;
private Node left, right;
Node(int data) {
this.data = data;
this.left = this.right = null;
}
}
}