题目描述:https://www.lintcode.com/problem/minimum-subtree/description
辅助函数用于计算子树和,全局函数更新记录最小子树和。
class Solution {
public:
TreeNode * res;
int sum;
TreeNode * findSubtree(TreeNode * root) {
if (root==NULL) return NULL;
sum=INT_MAX;
int sum=sumofSubtree(root);
return res;
}
int sumofSubtree(TreeNode * root){
if (root==NULL) return 0;
int left=sumofSubtree(root->left);
int right=sumofSubtree(root->right);
if (left+right+root->val<sum)
{sum=left+right+root->val;
res=root;}
return left+right+root->val;
}
};