112. Path Sum && 113. Path Sum II && 437. Path Sum III

来源:互联网 发布:python idle打开 编辑:程序博客网 时间:2024/06/15 03:22

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.

思路:用之前的DFS套路应该是可以的,但是这种求“有没有解”的问题,有直接递归的更简洁的方式,可以考虑把这种方式作为第二个树结构递归的模板?

public class Solution {    public boolean hasPathSum(TreeNode root, int sum) {                if(root == null)            return false;                if(root.val == sum && (root.right == null && root.left == null))            return true;                    return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);    }}





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]]

思路:就是DFS,注意是不是叶子节点的判断

import java.util.ArrayList;import java.util.List;public class Solution {List<List<Integer>> rst = new ArrayList<List<Integer>>();List<Integer> tmp = new ArrayList<Integer>();int target, cnt = 0;    public List<List<Integer>> pathSum(TreeNode root, int sum) {    //if(root == null)return rst;        target = sum;    dfs(root, 0);        return rst;    }private void dfs(TreeNode root, int sum) {if(root == null)return;if(root.left == null && root.right == null && sum+root.val == target) {        List<Integer> newList = new ArrayList<Integer>(tmp);        newList.add(root.val);rst.add(newList);return;}tmp.add(root.val);dfs(root.left, sum+root.val);tmp.remove(tmp.size()-1);tmp.add(root.val);dfs(root.right, sum+root.val);tmp.remove(tmp.size()-1);}}






You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8      10     /  \    5   -3   / \    \  3   2   11 / \   \3  -2   1Return 3. The paths that sum to 8 are:1.  5 -> 32.  5 -> 2 -> 13. -3 -> 11

思路:总体上就是按照步骤来模拟递归走的历程罢了,到了某个节点,如果我还没有开始,那我可以从以这个节点为开始(一旦开始中间就不能停了),也可以等到以后在开始

唯一注意的的是因为有重复计算,所以要处于2

package l437;public class Solution {public int rst = 0, sum = 0;    public int pathSum(TreeNode root, int sum) {    this.sum = sum;    dfs(root, 0, false);    return rst/2;    }private void dfs(TreeNode root, int i, boolean hasStarted) {if(i == sum && hasStarted) {rst++;// 这样写其实重复算了2遍,在某个root加进去满足条件后,其实left,right都会递归到这里}if(root == null)return;// 到下一个point有什么样的走法嘛,就模拟一下怎么走if(!hasStarted) {dfs(root.left, 0, false);dfs(root.right, 0, false);// 如果不想除2,在这里就要在这里判断root.val是不是sum,然后统一只加1dfs(root.left, root.val, true);dfs(root.right, root.val, true);} else {dfs(root.left, i+root.val, true);dfs(root.right, i+root.val, true);}}}


0 0
原创粉丝点击