Subproblem-based Approach for Resolving Max Value on Trees
Last updated
Last updated
There are two kinds of max problems on trees based on the approaches, for one type of problems, we usually use the subproblem strategy, while for the other type, we need to use cached DFS or DP.
The typical problems for the first category:
Max depth of the tree:
Diameter of the tree:
Max path sum:
The strategy is similar, we need to transfer the problem to subproblem on left and right children.
Max depth of the tree - node max depth = Math.max(left children depth, right children depth) + 1;
Diameter of the tree - node diameter = left children depth + right children depth;
Max path sum - node single side max path sum = left children single side max path sum + right children single side max path sum