Given a Binary Tree, find sum of all right leaves in it.
Similar article : Find sum of all left leaves in a given Binary Tree
Input : 1 / \ 2 3 / \ \ 4 5 8 \ / \ 2 6 7 Output : sum = 2 + 5 + 7 = 14
The idea is to traverse the tree starting from the root and check if the node is the leaf node or not. If the node is the right leaf than add data of right leaf to sum variable.
Following is the implementation for the same.
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find sum of all left leaves in a given Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Print all nodes in a binary tree having K leaves
- Minimum sum path between two leaves of a binary tree
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Height of binary tree considering even level leaves only
- Convert left-right representation of a binary tree to down-right
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Find first non matching leaves in two binary trees
- Minimum count of Full Binary Trees such that the count of leaves is N
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Find maximum among all right nodes in Binary Tree
- Check if all leaves are at same level
- Sum of nodes in the right view of the given binary tree
- Find multiplication of sums of data of leaves at same levels
- Find product of sums of data of leaves at same levels | Set 2
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print all leaf nodes of a binary tree from right to left