Minimum Path Sum of a Binary Tree

来源:互联网 发布:同志长得帅体验知乎 编辑:程序博客网 时间:2024/06/06 09:49


class TreeNode{int val;TreeNode left;TreeNode right;TreeNode(int x) { val = x; }}public class BSTMinimumPathSum {public static int minPathSum(TreeNode root) {if (root == null) {return 0;}if (root.left != null && root.right == null) {return root.val + minPathSum(root.left);}else if (root.left == null && root.right != null) {return root.val + minPathSum(root.right);}else {return root.val + Math.min(minPathSum(root.left), minPathSum(root.right));}}}