Skip to content

Commit e1b468b

Browse files
committed
sum of leaf nodes
1 parent 6cb1162 commit e1b468b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Trees/btreeLeafSum.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
class Node
3+
{
4+
int data;
5+
Node left;
6+
Node right;
7+
Node(int data)
8+
{
9+
this.data=data;
10+
left=right=null;
11+
}
12+
}
13+
14+
class BinaryTree{
15+
Node root;
16+
BinaryTree()
17+
{
18+
root = null;
19+
}
20+
BinaryTree(int data)
21+
{
22+
this.root=new Node(data);
23+
}
24+
int LsumBT(Node head)
25+
{
26+
27+
if(head==null)
28+
{
29+
return 0;
30+
}
31+
if(head.left==null && head.right==null)
32+
{
33+
return head.data;
34+
}
35+
return LsumBT(head.left)+LsumBT(head.right);
36+
}
37+
}
38+
39+
class btreeLeafSum{
40+
public static void main(String[] args) {
41+
42+
BinaryTree bt=new BinaryTree(2);
43+
bt.root.left=new Node(3);
44+
bt.root.right=new Node(5);
45+
bt.root.left.right=new Node(9);
46+
bt.root.right.left=new Node(7);
47+
System.out.println(bt.LsumBT(bt.root));
48+
}
49+
}

0 commit comments

Comments
 (0)