题目描述:
Given a binary tree, find the subtree with maximum average. Return the root of the subtree.
样例:
1
/ \
-5 11
/ \ / \
1 2 4 -2
return the node 11.
代码实现:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of binary tree
* @return the root of the maximum average of subtree
*/
private class ResultType {
int sum;
int size;
TreeNode root;
public ResultType(int sum, int size) {
this.sum = sum;
this.size = size;
}
}
TreeNode maxtree = null;
ResultType result = null;
public TreeNode findSubtree2(TreeNode root) {
helper(root);
return maxtree;
}
private ResultType helper(TreeNode root) {
if (root == null) {
return new ResultType(0, 0);
}
ResultType left = helper(root.left);
ResultType right = helper(root.right);
ResultType subresult = new ResultType(left.sum + right.sum + root.val,
left.size + right.size + 1);
if (maxtree == null || subresult.sum * result.size > subresult.size * result.sum) {
maxtree = root;
result = subresult;
}
return subresult;
}
}