public class Solution {
/*
* @param root: the root of binary tree
* @return: the maximum weight node
*/
public TreeNode result = null;
public int maximum_weight = Integer.MIN_VALUE;
public TreeNode findSubtree(TreeNode root) {
// Write your code here
helper(root);
return result;
}
public int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left_weight = helper(root.left);
int right_weight = helper(root.right);
if (result == null || left_weight + right_weight + root.val > maximum_weight) {
maximum_weight = left_weight + right_weight + root.val;
result = root;
}
return left_weight + right_weight + root.val;
}
}
网友评论