LeetCode.112(113/437) Path Sum I && II && III
2017-12-29 23:09
519 查看
题目112:
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
return true, as there exist a root-to-leaf path
22.
分析:
题目113:
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
return
分析:
题目437:
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:
分析:
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->2which sum is
22.
分析:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean hasPathSum(TreeNode root, int sum) { //给定二叉树,判断是否存在路径之和为给定的值target //思路:利用栈来存储之前的路径的结果,之后对左右子树进行递归 if(root==null) return false; if(root.right==null&&root.left==null&&root.val==sum) return true; else{ return hasPathSum(root.right,sum-root.val)||hasPathSum(root.left,sum-root.val); } } }
题目113:
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] ]
分析:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum) { //给定二叉树,返回路径总和为给定值得集合 //思路:使用一个stack存储之前已经访问过的路径,当访问到叶子节点时,且remain为0,就将该数据遍历给subList List<List<Integer>> list=new ArrayList<>(); Stack<Integer> stack=new Stack<Integer>(); if(root==null) return list; backtrace(list,root,sum,stack); return list; } //递归 public void backtrace(List<List<Integer>> list,TreeNode root,int remain,Stack<Integer> path){ if(root==null) return ; else if(root.right==null&&root.left==null){ if(root.val==remain){ List<Integer> subList=new ArrayList<>(); for(int i:path){ subList.add(i); } subList.add(root.val); list.add(subList); } }else{ //继续递归查找 path.push(root.val); backtrace(list,root.left,remain-root.val,path); backtrace(list,root.right,remain-root.val,path); //不符合条件,则弹出该节点值 path.pop(); } } }
题目437:
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 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3 2. 5 -> 2 -> 1 3. -3 -> 11
分析:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public int pathSum(TreeNode root, int sum) { //给定二叉树,求其中满足路径之和为sum的路径一共多有少条(不一定从根节点到叶子结点,可以是其中一段)。 //思路:使用递归求解,使用类似数组求子集之和,使用remain来作为递归求解的sum if(root==null) return 0; return pathSumFrom(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum); } //递归 public int pathSumFrom(TreeNode root,int sum){ //递归求解 if(root==null) return 0; //如果当前节点的值就为目标值,则加1 return (root.val==sum?1:0)+pathSumFrom(root.left,sum-root.val)+pathSumFrom(root.right,sum-root.val); } }
相关文章推荐
- 【LeetCode】112,113,437. Path Sum I, II, III
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
- 113-Path Sum II
- 113. Path Sum II
- leetcode 113 Path Sum II
- [leetcode 113] Path Sum II
- Java for LeetCode 113 Path Sum II
- Leetcode-113 Path Sum II
- 【LeetCode-面试算法经典-Java实现】【113-Path Sum II(路径和)】
- LeetCode | Unique Paths & II & Minimum Path Sum
- 113. Path Sum II
- path-sum i & ii
- 【LeetCode】(113)Path Sum II(Medium)
- Leetcode 笔记 113 - Path Sum II
- [437]. Path Sum III,[687]. Longest Univalue Path
- 113. Path Sum II
- 113. Path Sum II
- 【LeetCode】path-sum i&ii
- LeetCode113—Path Sum II
- Container With Most Water & Path Sum I && II