596.Minimum Subtree
1.Description(Easy)
1
/ \
-5 2
/ \ / \
0 2 -4 -52.Code
public TreeNode node=null;
public int minsum=Integer.MAX_VALUE;
public TreeNode findSubtree(TreeNode root){
helper(root);
return node;
}
public int helper(TreeNode root){
if(root==null){
return 0;
}
int left=helper(root.left);
int right=helper(root.right);
int sum=root.val+left+right;
if(sum<minsum){
minsum=sum;
node=root;
}
return sum;
}Last updated