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 firstname.lastname@example.org. 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.
- Find sum of all left leaves in a given Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Height of binary tree considering even level leaves only
- 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
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Find first non matching leaves in two binary trees
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Find LCA in Binary Tree using RMQ
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Find the median array for Binary tree
- Find the maximum GCD of the siblings of a Binary Tree
- Find the Deepest Node in a Binary Tree
- Find maximum level sum in Binary Tree
- Find maximum (or minimum) in Binary Tree
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find the Level of a Binary Tree with Width K