Skip to content

Instantly share code, notes, and snippets.

@swapnala
Last active February 8, 2017 07:12
Show Gist options
  • Save swapnala/ec7c9e8483f34c3bf70b14012fcc8c2b to your computer and use it in GitHub Desktop.
Save swapnala/ec7c9e8483f34c3bf70b14012fcc8c2b to your computer and use it in GitHub Desktop.
Find the minimum path sum for Binary Search Tree (From root to leaf)
public static int minPathSum(TreeNode root) {
if(root == null) return 0;
int sum = root.val;
int leftSum = Integer.MAX_VALUE;
int rightSum = Integer.MAX_VALUE;
if(root.right==null && root.left==null) {
return sum;
}else{
if(root.left!=null){
leftSum = minPathSum(root.left);
}
if (root.right!=null){
rightSum = minPathSum(root.right);
}
if(leftSum < rightSum){
sum += leftSum;
}else{
sum += rightSum;
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment