Find out the maximum cost of all the possible root-to-leaf paths

1
2
3
4
5
6
7
8
9
10
11
12
public class solution{
public int maxPathCost(Node root){
if (root == null)
return 0;
if (root.left == null && root.right == null)
return root.value;

int leftMax = maxPathCost(root.left);
int rightMax = maxPathCost(root.right);
return root.val + leftMax > rightMax ? leftMax : rightMax;
}
}