forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSum.java
More file actions
73 lines (64 loc) · 1.4 KB
/
Copy pathtreeSum.java
File metadata and controls
73 lines (64 loc) · 1.4 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Node
{
int data;
Node left;
Node right;
Node(int data)
{
this.data=data;
left=right=null;
}
}
class BinaryTree{
Node root;
BinaryTree()
{
root = null;
}
BinaryTree(int data)
{
this.root=new Node(data);
}
int sumBT(Node head)
{
if(head==null)
{
return 0;
}
return head.data+ sumBT(head.left)+sumBT(head.right);
}
//method 2
static int sum=0;
public int sumBT1(Node root)
{
sum=0;
return sum(root);
}
public int sum(Node root){
if(root == null)
return 0;
sum(root.left);
sum(root.right);
sum += root.data;
return sum;
}
}
class treeSum{
public static void main(String[] args) {
BinaryTree bt=new BinaryTree(2);
bt.root.left=new Node(3);
bt.root.right=new Node(5);
bt.root.left.right=new Node(9);
bt.root.right.left=new Node(7);
System.out.println(bt.sumBT(bt.root));
System.out.println(bt.sumBT1(bt.root));
//2nd binary tree
BinaryTree bt1=new BinaryTree(2);
bt1.root.left=new Node(3);
bt1.root.right=new Node(10);
bt1.root.left.right=new Node(9);
bt1.root.right.left=new Node(6);
System.out.println(bt1.sumBT(bt1.root));
System.out.println(bt1.sumBT1(bt1.root));
}
}