/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> rst = new ArrayList<List<Integer>>();
if(root == null) return rst;
dfs(rst, root, new ArrayList<Integer>(), 0, sum);
return rst;
}
private void dfs(List<List<Integer>> rst, TreeNode root, List<Integer> list, int curSum, int targetSum){
if(root == null) return;
list.add(root.val);
curSum += root.val;
if(root.left == null && root.right == null){
if(curSum == targetSum){
rst.add(new ArrayList<Integer>(list));
list.remove(list.size() - 1);
return;
}
}
dfs(rst, root.left, list, curSum, targetSum);
dfs(rst, root.right, list, curSum, targetSum);
curSum -= root.val;
list.remove(list.size() - 1);
}
}
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> rst = new ArrayList<List<Integer>>();
dfs(rst, new ArrayList<Integer>(), root, sum);
return rst;
}
private void dfs(List<List<Integer>> rst, List<Integer> list, TreeNode root, int sum){
if(root == null) return;
if(root.left == null && root.right == null){
if(root.val == sum){
list.add(root.val);
rst.add(new ArrayList<Integer>(list));
list.remove(list.size() - 1);
return;
} else {
return;
}
}
list.add(root.val);
dfs(rst, list, root.left, sum - root.val);
dfs(rst, list, root.right, sum - root.val);
list.remove(list.size() - 1);
}
}
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
return dfs(root, 0, sum);
}
private boolean dfs(TreeNode root, int curSum, int targetSum){
if(root == null) return false;
curSum += root.val;
if(root.left == null && root.right == null){
if(curSum == targetSum) return true;
}
boolean left = dfs(root.left, curSum, targetSum);
boolean right = dfs(root.right, curSum, targetSum);
curSum -= root.val;
return (left || right);
}
}
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
if(root.left == null && root.right == null && root.val == sum) return true;
return (hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val));
}
}
5
/ \
1 -1
/ \ / \
0 -2 3 2
public class Solution {
// 全局变量,用于连接不连续的状态
int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfsBottomUp(root);
return max;
}
private int dfsBottomUp(TreeNode root){
if(root == null) return 0;
// 检查两边的做大路径和,或者直接抛弃(取值为0)
// 因此当一个小三角形一边为负数的时候
// 最后返回的结果看起来是三个点的和,其实只是一条边
int left = Math.max(0, dfsBottomUp(root.left));
int right = Math.max(0, dfsBottomUp(root.right));
// 检查通过当前 “root” 的三角形路线(拐弯)
// 不需要单独再取 Left / Right 中的最大值
// 因为在 Bottom-Up 的递归中左右子树的最大路径已经被更新过了
// 即其中某个分支为负时,最大子树和 = 最大路径和
max = Math.max(max, left + right + root.val);
// 传递到上一层的路线必须连续且不能拐弯,保持连续的递归状态
return Math.max(left, right) + root.val;
}
}
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
private class ResultType {
// singlePath: 从root往下走到任意点的最大路径,至少包含一个点
// maxPath: 从树中任意到任意点的最大路径,这条路径至少包含一个点
int singlePath, maxPath;
ResultType(int singlePath, int maxPath) {
this.singlePath = singlePath;
this.maxPath = maxPath;
}
}
private ResultType helper(TreeNode root) {
if (root == null) {
return new ResultType(Integer.MIN_VALUE, Integer.MIN_VALUE);
}
// Divide
ResultType left = helper(root.left);
ResultType right = helper(root.right);
// Conquer
int singlePath =
Math.max(0, Math.max(left.singlePath, right.singlePath)) + root.val;
int maxPath = Math.max(left.maxPath, right.maxPath);
maxPath = Math.max(maxPath,
Math.max(left.singlePath, 0) +
Math.max(right.singlePath, 0) + root.val);
return new ResultType(singlePath, maxPath);
}
public int maxPathSum(TreeNode root) {
ResultType result = helper(root);
return result.maxPath;
}
}
public class Solution {
public int sumNumbers(TreeNode root) {
return dfs(root, 0);
}
// 把当前考虑的节点作为参数的 dfs 结构
private int dfs(TreeNode root, int num){
// 只在叶节点上做计算,这里说明不是有效 path
if(root == null) return 0;
-------------ADD----------------
num += root.val;
------------Leaf Node-----------
if(root.left == null && root.right == null){
return num;
}
------------DFS------------------
int left = dfs(root.left, num * 10);
int right = dfs(root.right, num * 10);
--------Backtracking------------
num -= root.val;
return left + right;
}
}
public class Solution {
public int sumNumbers(TreeNode root) {
return dfs(root, 0);
}
private int dfs(TreeNode root, int num){
if(root == null) return 0;
if(root.left == null && root.right == null){
return num + root.val;
}
int left = dfs(root.left, (num + root.val) * 10);
int right = dfs(root.right, (num + root.val) * 10);
return left + right;
}
}