Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from root node.
1 / \ 2 3 / \ / \ 4 5 6 7 Input : root(of above tree) Output : 22 Explanation: Nodes at maximum depth are: 4, 5, 6, 7. So, sum of these nodes = 22
While traversing the nodes compare the level of the node with max_level (Maximum level till the current node).If the current level exceeds the maximum level, update the max_level as current level.If the max level and current level are same ,add the root data to current sum.if level is less than max_level, do nothing.
This article is contributed by Ashwin Loganathan. 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.
Approach: Calculate the max depth of the given tree. Now, start traversing the tree similarly as traversed during maximum depth calculation. But, this time with one more argument (i.e. maxdepth), and traverse recursively with decreasing depth by 1 for each left or right call. Wherever max == 1, means the node at max depth is reached. So add its data value to sum. Finally, return sum.
Below is the implementation for above approach:
# Python3 code for sum of nodes at maximum depth
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to find the sum of nodes at maximum depth
# arguments are node and max, where Max is to match
# the depth of node at every call to node, if Max
# will be equal to 1, means we are at deepest node.
def sumMaxLevelRec(node, Max):
# base case
if node == None:
# Max == 1 to track the node at deepest level
if Max == 1:
# recursive call to left and right nodes
return (sumMaxLevelRec(node.left, Max – 1) +
sumMaxLevelRec(node.right, Max – 1))
# call to function to calculate max depth
MaxDepth = maxDepth(root)
return sumMaxLevelRec(root, MaxDepth)
# maxDepth function to find
# the max depth of the tree
# base case
if node == None:
# Either leftDepth of rightDepth is
# greater add 1 to include height
# of node at which call is
return 1 + max(maxDepth(node.left),
# Driver code
if __name__ == “__main__”:
# Constructing tree
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
# call to calculate required sum
# This code is contributed by Rituraj Jain
Time Complexity: O(N), where N is the number of nodes in the tree.
- Tree Traversals (Inorder, Preorder and Postorder)
- Find the node with minimum value in a Binary Search Tree
- Write a program to Calculate Size of a tree | Recursion
- Write a Program to Find the Maximum Depth or Height of a Tree
- Write a program to Delete a Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Convert a Binary Tree into its Mirror Tree
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Lowest Common Ancestor in a Binary Search Tree.
- The Great Tree-List Recursion Problem.
- Check sum of Covered and Uncovered nodes of Binary Tree
- Level Order Tree Traversal
- Program to count leaf nodes in a binary tree
- A program to check if a binary tree is BST or not
- Check for Children Sum Property in a Binary Tree
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.