Last active
February 8, 2017 07:12
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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