
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Sum of Leaf Nodes in Binary Tree
We can find the sum of leaf nodes in a binary tree using an iterative and a recursive approach. This problem has many real-life applications, such as analyzing hierarchies, calculating final results in decision trees, or summing final nodes in various kinds of trees used in computer algorithms.
In this article, we are going to learn how can find the sum of all leaf nodes in a binary tree in the C++ language.
What are Leaf Nodes in a Binary Tree?
The leaf nodes of a binary tree are the nodes that do not have any children. Both the left and right pointers of a leaf node are NULL. Here is an example displaying the leaf nodes of a binary tree:
10 / \ 5 15 / \ \ 3 7 18 The leaf nodes of the given Binary Tree are: 3, 7, 18 Output: The sum of leaf nodes is: 3 + 7 + 18 = 28
Below are the approaches using which we can find the sum of leaf nodes of a binary tree:
Using Iterative Approach
In this approach, we first create a queue and push the root node into it. Then, we keep removing nodes from the queue one by one, and check if it's a leaf node; then we add its value to our sum. If it has children, we add those children to the queue for further checking. We continue until the queue becomes empty.
- We first define a structure for the binary tree nodes.
- Use a queue to perform level-order traversal.
- Now, check if a node is a leaf node and add it to the sum.
- Traverse all leaf nodes and return the calculated sum.
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(NULL), right(NULL) {} }; int sumOfLeafNodes(Node* root) { if (!root) return 0; int sum = 0; queue<Node*> q; q.push(root); while (!q.empty()) { Node* current = q.front(); q.pop(); if (!current->left && !current->right) // If leaf node sum += current->data; if (current->left) q.push(current->left); if (current->right) q.push(current->right); } return sum; } int main() { Node* root = new Node(10); root->left = new Node(5); root->right = new Node(15); root->left->left = new Node(3); root->left->right = new Node(7); root->right->right = new Node(18); cout << "Sum of leaf nodes: " << sumOfLeafNodes(root) << endl; return 0; }
The output of the above code is:
Sum of leaf nodes: 28
Using Recursive Approach
In this approach, we use a recursive method to traverse the binary tree. Here are the steps:
- Define a structure for a binary tree node.
- Now, create a recursive function that returns 0 if the node is null.
- If it reaches at last level, i.e., leaf nodes, return the node's value.
- Recursively call the function for the left and right subtrees.
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left; Node* right; Node(int val) : data(val), left(NULL), right(NULL) {} }; int sumOfLeafNodes(Node* root) { if (!root) return 0; // Base case if (!root->left && !root->right) return root->data; // If leaf node return sumOfLeafNodes(root->left) + sumOfLeafNodes(root->right); } int main() { Node* root = new Node(10); root->left = new Node(5); root->right = new Node(15); root->left->left = new Node(3); root->left->right = new Node(7); root->right->right = new Node(18); cout << "Sum of leaf nodes: " << sumOfLeafNodes(root) << endl; return 0; }
The output of the above code is:
Sum of leaf nodes: 28
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Iterative Approach | O(n) | O(n) |
Recursive Approach | O(n) | O(h) |