File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments