Problem
Given a binary tree where each node contains positive numbers, find the maximum sum that is possible along any path of the binary tree.
Given a binary tree where each node contains positive numbers, find the maximum sum that is possible along any path of the binary tree.
Solution
We will solve this recursively. The function f(node) will return the maximum sum of the tree rooted at the node. So f(node)=node.value+ max(f(node.left),f(node.right)). f(node.left) and f(node.right) will be calculated recursively. For f(null) we will return 0;
We will solve this recursively. The function f(node) will return the maximum sum of the tree rooted at the node. So f(node)=node.value+ max(f(node.left),f(node.right)). f(node.left) and f(node.right) will be calculated recursively. For f(null) we will return 0;
No comments:
Post a Comment