Tags: "leetcode", "binary-tree", access_time 1-min read

Edit this post on Github

Minimum Path Sum in Binary Tree

Created: March 27, 2019 by [lek-tin]

Last updated: March 27, 2019

Given a Binary Tree, find the minimum sum path from a leaf to root. For example, in the following tree, there are three leaf to root paths 8->-2->10, -4->-2->10 and 7->10. The sums of these three paths are 16, 4 and 17 respectively. The minimum of them is 17 and the path for minimum is -4->-2->10.

          10
        /      \
      -2        7
    /   \     
  8     -4    

Solution

public class PathSum {
    public int Solution(TreeNode root) {
        if (root == null) return 0;
        if (root.left == null && root.right == null) return root.val;
        return Math.min(Solution(root.left), Solution(root.right)) + root.val;
    }
}