forked from atulim/Java-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Root.java
41 lines (26 loc) · 1.12 KB
/
Root.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
/* This program is to find value of root node in a tree
when value of each node and sum of child nodes of all nodes is given. */
package javacodes; //encapsulates a group of classes into a package called javacodes
import java.util.*; //import package of util class
public class Root{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Number of vertices in tree");
int v=in.nextInt(); //taking number of vertices as input
int sumt=0,sumc=0,tmp;
System.out.println("Values of vertices");
for(int i=1;i<=v;i++)
{
tmp=in.nextInt(); //taking values of each node as input
sumt+=tmp;
}
System.out.println("Enter sum of child's values");
for(int i=1;i<=v;i++)
{
tmp=in.nextInt();
sumc+=tmp;
}
System.out.println("Value of root node is :"+(sumt-sumc));
}
}