Skip to content

Commit c429126

Browse files
authored
Count Leaves in Binary Tree
1 parent a93905e commit c429126

File tree

1 file changed

+52
-177
lines changed

1 file changed

+52
-177
lines changed
Lines changed: 52 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,52 @@
1-
/* Geeks for Geeks */
2-
/* Title - Count Leaves in Binary Tree */
3-
/* Created By - Akash Modak */
4-
/* Date - 11/10/2020 */
5-
6-
// Given a Binary Tree of size N , You have to count leaves in it. For example, there are two leaves in following tree
7-
8-
// 1
9-
// / \
10-
// 10 39
11-
// /
12-
// 5
13-
14-
// Input:
15-
// First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
16-
17-
// The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
18-
19-
// For example:
20-
21-
// For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
22-
23-
// Output:
24-
// For each test case print the count of leaves.
25-
// Your Task:
26-
// You don't have to take input. Complete the function countLeaves() that takes root node of given tree as parameter and returns the count of leaves in tree . The printing is done by the driver code.
27-
// Constraints:
28-
// 1<= T <= 30
29-
// 1<= N <= 104
30-
// Example:
31-
// Input:
32-
// 2
33-
// 3 4 2
34-
// 4 8 10 7 N 5 1 3
35-
// Output:
36-
// 2
37-
// 3
38-
// Explanation:
39-
// Test Case 2: Given Tree is
40-
// 4
41-
// / \
42-
// 8 10
43-
// / / \
44-
// 7 5 1
45-
// /
46-
// 3
47-
// Three leaves are 3 , 5 and 1.
48-
49-
// Note:The Input/Ouput format and Example given below is used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.
50-
51-
// { Driver Code Starts
52-
#include <bits/stdc++.h>
53-
using namespace std;
54-
55-
struct Node
56-
{
57-
int data;
58-
struct Node *left;
59-
struct Node *right;
60-
};
61-
Node* newNode(int val)
62-
{
63-
Node* temp = new Node;
64-
temp->data = val;
65-
temp->left = NULL;
66-
temp->right = NULL;
67-
68-
return temp;
69-
}
70-
Node* buildTree(string str)
71-
{
72-
// Corner Case
73-
if(str.length() == 0 || str[0] == 'N')
74-
return NULL;
75-
76-
// Creating vector of strings from input
77-
// string after spliting by space
78-
vector<string> ip;
79-
80-
istringstream iss(str);
81-
for(string str; iss >> str; )
82-
ip.push_back(str);
83-
84-
// Create the root of the tree
85-
Node* root = newNode(stoi(ip[0]));
86-
87-
// Push the root to the queue
88-
queue<Node*> queue;
89-
queue.push(root);
90-
91-
// Starting from the second element
92-
int i = 1;
93-
while(!queue.empty() && i < ip.size()) {
94-
95-
// Get and remove the front of the queue
96-
Node* currNode = queue.front();
97-
queue.pop();
98-
99-
// Get the current node's value from the string
100-
string currVal = ip[i];
101-
102-
// If the left child is not null
103-
if(currVal != "N") {
104-
105-
// Create the left child for the current node
106-
currNode->left = newNode(stoi(currVal));
107-
108-
// Push it to the queue
109-
queue.push(currNode->left);
110-
}
111-
112-
// For the right child
113-
i++;
114-
if(i >= ip.size())
115-
break;
116-
currVal = ip[i];
117-
118-
// If the right child is not null
119-
if(currVal != "N") {
120-
121-
// Create the right child for the current node
122-
currNode->right = newNode(stoi(currVal));
123-
124-
// Push it to the queue
125-
queue.push(currNode->right);
126-
}
127-
i++;
128-
}
129-
130-
return root;
131-
}
132-
int countLeaves(struct Node* root);
133-
134-
int main()
135-
{
136-
int t;
137-
scanf("%d ",&t);
138-
while(t--)
139-
{
140-
string s;
141-
getline(cin,s);
142-
Node* root = buildTree(s);
143-
cout<< countLeaves(root)<<endl;
144-
}
145-
return 0;
146-
}
147-
// } Driver Code Ends
148-
149-
150-
//User function Template for C++
151-
152-
/* A binary tree node has data, pointer to left child
153-
and a pointer to right child
154-
struct Node
155-
{
156-
int data;
157-
Node* left;
158-
Node* right;
159-
}; */
160-
161-
/* Should return count of leaves. For example, return
162-
value should be 2 for following tree.
163-
10
164-
/ \
165-
20 30 */
166-
int countLeaves(Node* root)
167-
{
168-
// Your code here
169-
if(root==NULL)
170-
return 0;
171-
172-
if(root->left==NULL and root->right==NULL)
173-
return 1;
174-
int l = countLeaves(root->left);
175-
int r = countLeaves(root->right);
176-
return l+r;
177-
}
1+
/* Geeks for Geeks */
2+
/* Title - Count Leaves in Binary Tree */
3+
/* Created By - Akash Modak */
4+
/* Date - 23/08/2021 */
5+
6+
// Given a Binary Tree of size N , You have to count leaves in it. For example, there are two leaves in following tree
7+
8+
// 1
9+
// / \
10+
// 10 39
11+
// /
12+
// 5
13+
14+
15+
16+
// Example 1:
17+
18+
19+
// Input:
20+
// Given Tree is
21+
// 4
22+
// / \
23+
// 8 10
24+
// / / \
25+
// 7 5 1
26+
// /
27+
// 3
28+
// Output:
29+
// 3
30+
// Explanation:
31+
// Three leaves are 3 , 5 and 1.
32+
33+
34+
// Your Task:
35+
// You don't have to take input. Complete the function countLeaves() that takes root node of given tree as parameter and returns the count of leaves in tree . The printing is done by the driver code.
36+
37+
38+
int countLeaves(Node* root)
39+
{
40+
// Your code here
41+
42+
if(root == NULL){
43+
return 0;
44+
}
45+
46+
if(root->left == NULL and root->right == NULL){
47+
return 1;
48+
}
49+
50+
return countLeaves(root->left) + countLeaves(root->right);
51+
52+
}

0 commit comments

Comments
 (0)