Description
Given a binary tree, find the subtree with maximum average. Return the root of the subtree.
Example
Given a binary tree:
1 |
1 |
return the node 11
.
Thoughts
Let’s first usee DFS recursive with divide and conquer approach. Since average = sum / count
, lets creates a new class for return type called ResultType
to store both sum
and count
.
1 |
private class { |
Recursive DFS (Divide and Conquer, Bottom-up)
A helper method is also needed since the original method only return TreeNode
.
1 |
|
Globals can be eliminated by adding them into the input arguements.
1 |
public ResultType getSum(TreeNode node, TreeNode maxRoot, ResultType max) { |
Summary
- Recursive DFS (Divide and Conquer, Bottom-up)
Logs
- 02/05/2019: Solution added
近期评论