diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 425be2db..314e0019 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -139,6 +139,7 @@ - [Finding the elements of a tree visible from top view](Trees/Top-View-Of-A-Tree.cpp) - [Binary Tree Implementation](Trees/binary-tree-implementation.cpp) - [Iterative Segment Tree](Trees/IterativeSegmentTree.cpp) +- [Sum of right leaves](Trees/sum-of-right-leaves.cpp) ## Trie diff --git a/algorithms/CPlusPlus/Trees/sum-of-right-leaves.cpp b/algorithms/CPlusPlus/Trees/sum-of-right-leaves.cpp new file mode 100644 index 00000000..99e7d6d1 --- /dev/null +++ b/algorithms/CPlusPlus/Trees/sum-of-right-leaves.cpp @@ -0,0 +1,81 @@ +// The sum of right leaves algorithm uses a queue to navigate through a binary tree +// Worst Case Time Complexity: O(n) +// Average Time Complexity: O(n) + +#include +#include + +using namespace std; + +// Node structure for tree +struct Node { + int val; + Node *left; + Node *right; + + Node(int val) { + this->val = val; + left = nullptr; + right = nullptr; + } +}; + +int sumRightLeaves(Node* root) { + queue q; + int rightSum = 0; + + // Checking if the root is nullptr push to queue if it exists + if(root) + { + q.push(root); + } + + while(!q.empty()) + { + // Check if there exists a right node + if(q.front()->right) + { + // Check if this is a leaf node + if(q.front()->right->right == nullptr && q.front()->right->left == nullptr) + { + rightSum += q.front()->right->val; + } + else + { + q.push(q.front()->right); + } + } + if(q.front()->left) // Check down left side of tree + { + q.push(q.front()->left); + } + + q.pop(); + } + + return rightSum; +} + +int main() +{ + Node* root = new Node(3); + root->left = new Node(5); + root->right = new Node(7); + root->left->left = new Node(8); + root->left->right = new Node(10); + root->right->right = new Node(13); + +// 3 +// / \ +// 5 7 +// / \ \ +// 8 10 13 +// +// Sample Output +// Sum of the right leaves: 23 + +// Outputting sum of right leaves + cout << "Sum of right leaves: " << sumRightLeaves(root); + + return 0; +} \ No newline at end of file