Given a binary tree containing n nodes. The problem is to find the sum of all nodes on the longest path from root to leaf node. If two or more paths compete for the longest path, then the path having maximum sum of nodes is being considered.
Input : Binary tree: 4 / \ 2 5 / \ / \ 7 1 2 3 / 6 Output : 13 4 / \ 2 5 / \ / \ 7 1 2 3 / 6 The highlighted nodes (4, 2, 1, 6) above are part of the longest root to leaf path having sum = (4 + 2 + 1 + 6) = 13
Approach: Recursively find the length and sum of nodes of each root to leaf path and accordingly update the maximum sum.
sumOfLongRootToLeafPath(root, sum, len, maxLen, maxSum) if root == NULL if maxLen < len maxLen = len maxSum = sum else if maxLen == len && maxSum is less than sum maxSum = sum return sumOfLongRootToLeafPath(root-left, sum + root-data, len + 1, maxLen, maxSum) sumOfLongRootToLeafPath(root-right, sum + root-data, len + 1, maxLen, maxSum) sumOfLongRootToLeafPathUtil(root) if (root == NULL) return 0 Declare maxSum = Minimum Integer Declare maxLen = 0 sumOfLongRootToLeafPath(root, 0, 0, maxLen, maxSum) return maxSum
Sum = 13
Time Complexity: O(n)
This article is contributed by Ayush Jauhari. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Root to leaf path with maximum distinct nodes
- Print the longest path from root to leaf in a Binary tree
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Print the longest leaf to leaf path in a Binary tree
- GCD from root to leaf path in an N-ary tree
- Check if there is a root to leaf path with given sequence
- Root to leaf path sum equal to a given number
- Shortest root to leaf path sum equal to a given number
- Root to leaf path product equal to a given number
- Print the first shortest root to leaf path in a Binary Tree
- Find the maximum sum leaf to root path in a Binary Tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Remove nodes on root to leaf paths of length < K
- Print the path common to the two paths from the root to the two given nodes
- Find the path from root to the given nodes of a tree for multiple queries
- Print path from root to all nodes in a Complete Binary Tree
- Print the nodes that are just above the leaf node
- Maximum XOR with given value in the path from root to given node in the tree
- Print all nodes that are at distance k from a leaf node