Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null) {
return sum == root.val;
}
return hasPathSum (root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if(root == null){
return result;
}
List<Integer> list = new ArrayList<Integer>();
helper(root, result, list, sum, 0);
return result;
}
private void helper(TreeNode root, List<List<Integer>> result, List<Integer> list, int sum, int cur){
list.add(root.val);
cur += root.val;
if(root.left == null && root.right == null){
if(sum == cur){
result.add(new ArrayList<Integer>(list));
}
return;
}
if(root.left != null){
helper(root.left, result, list, sum, cur);
list.remove(list.size() - 1);
}
if(root.right != null){
helper(root.right, result, list, sum, cur);
list.remove(list.size() - 1);
}
}
}
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
这里所求的最大路径sum是任意一段路径的sum。所以有几种情况需要讨论。
4 -4
/ \ / \
1 -3 1 -3
/ \ / \
2 3 2 3
在这个例子中,左边的max path sum是1 + 3 + 4 = 8,而右边的max path sum是 1 + 2 + 3 = 6。有折返意味着当前路径不可能包含它的parent了,就如左边的例子max path sum不应该是 1 + 2 + 3 + 4而是1 + 3 + 4。
所以这里我们需要记录两个max的值:1.单边路径的最大值。2.整体路径最大值。
因此要返回两个值,所以我们这里用一个wrapper class做返回类型。
public class Solution {
class ResultType{
int single;
int max;
public ResultType(int a, int b){
this.single = a;
this.max = b;
}
}
public int maxPathSum(TreeNode root) {
if(root == null){
return 0;
}
ResultType result = getMaxPathSum(root);
return result.max;
}
private ResultType getMaxPathSum(TreeNode root){
if(root == null){
return new ResultType(0, Integer.MIN_VALUE);
}
ResultType left = getMaxPathSum(root.left);
ResultType right = getMaxPathSum(root.right);
//我们不可能选择负的单边路径,所以最小值是0.
int val = Math.max(0, Math.max(left.single, right.single) + root.val);
//整体路径的最大值可能是左子树或者右子树的整体路径最大值
int max = Math.max(left.max, right.max);
//或者是有折返的路径最大值
max = Math.max(left.single + right.single + root.val, max);
return new ResultType(val, max);
}
}
这里其实我们可以不用wrapper class,我可以设一个全局变量来记录结果,在遍历的时候实时更新全局变量,而递归函数只返回单边路径最大值。
public class Solution {
private int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
maxSum(root);
return max;
}
private int maxSum(TreeNode root){
if(root == null){
return 0;
}
int left = Math.max(0, maxSum(root.left));
int right = Math.max(0, maxSum(root.right));
max = Math.max(max, left + right + root.val);
return Math.max(0, Math.max(left, right) + root.val);
}
}