-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSumOfLeftLeaves.java
94 lines (84 loc) · 2.18 KB
/
SumOfLeftLeaves.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Sum up left leaves of the tree
You are given root of the binary tree,
your task is to find the sum of all the leaves
present on the left.
*/
import java.io.*;
import java.until.*;
//defining a class for storing nodes of tree
class BTNode
{
int value;
BTNode left;
BTNode right;
BTNode()
{}
BTNode(int value)
{
this.value = value;
}
BTNode(int value, BTNode left, BTNode right)
{
this.value = value;
this.left = left;
this.right = right;
}
}
public class SumOfLeftLeaves
{
//function to find the sum of left leaves
public int sumLeftLeaves(BTNode root)
{
//if the root is null then simply return 0
if(root == null)
{
return 0;
}
//else if the left node does not have any
//child then we add that node to the sum
//derived from right side
else if(root.left != null && root.left.left == null && root.left.right == null)
{
return root.left.val + sumLeftLeaves(root.right);
}
//else we recursivily call the function for both the sides
else
{
return sumLeftLeaves(root.left) + sumLeftLeaves(root.right);
}
}
//driver code
public static void main()
{
// Taking input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the values of nodes for tree : ");
String str = br.readLine();
String ip[] = str.split(" ");
// Here we start creating the root of the tree
Node root = new Node(Integer.parseInt(ip[0]));
// Pushing the roots to the queue
Queue<Node> Treequeue = new LinkedList<>();
Treequeue.add(root);
//for output
System.out.println("Sum of left leaves is : ");
System.out.print(sumLeftLeaves(root));
System.out.println(" ");
}
}
/*
EXAMPLE --
INPUT--
Enter the values of nodes for tree : 3 9 20 null null 15 7
OUTPUT--
Sum of left leaves is : 24
3
/ \
9 20
/ \
15 7
SUM = 9 + 15 = 24
TIME COMPLEXITY --> O(log N)
SPACE COMPLEXITY --> O(N) , where N is the number of nodes.
*/