- 浏览: 184862 次
- 性别:
- 来自: 济南
文章分类
最新评论
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]
]
与Path Sum相比,这道题要求我们输出所有可能的结果,我们同样采用DFS,因为要输出所有可能的结果,我们用回溯法来记录,当找到符合条件的路径就记录下来,然后回溯,继续查找其它的路径。代码如下:
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]
]
与Path Sum相比,这道题要求我们输出所有可能的结果,我们同样采用DFS,因为要输出所有可能的结果,我们用回溯法来记录,当找到符合条件的路径就记录下来,然后回溯,继续查找其它的路径。代码如下:
/** * 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>> result = new ArrayList<List<Integer>>(); LinkedList<Integer> list = new LinkedList<Integer>(); if(root == null) return result; getPath(root, 0, sum, result, list); return result; } public void getPath(TreeNode root, int count, int sum, List<List<Integer>> result, LinkedList<Integer> list) { if(root == null) return; count += root.val; list.add(root.val); if(count == sum && root.left == null && root.right == null) { result.add(new LinkedList<Integer>(list)); list.removeLast(); return; } getPath(root.left, count, sum, result, list); getPath(root.right, count, sum, result, list); list.removeLast(); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
113. 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. Note: A leaf is a node with no children. Example: Given the below binary tree...
java java_leetcode-113-path-sumII
java java_leetcode题解之Path Sum III.java
python python_leetcode题解之113_Path_Sum_II
javascript js_leetcode题解之113-path-sum-ii.js
此外,文档还提到了另一个问题——路径总和II(Path Sum II),这是一个寻找二叉树中特定和的路径的问题。虽然没有给出完整的代码,但可以看出这个问题涉及到递归或深度优先搜索(DFS)的策略,可能需要使用回溯法来...
public int PathSum(TreeNode root, int sum) { _count = 0; _sum = sum; Travel(root, new List()); return _count; } private void Travel(TreeNode current, List<int> ret) { if (current == null) { ...
java java_leetcode-112-path-sum
python python_leetcode题解之112_Path_Sum
- **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to Linked List**:将二叉树展平为单链表。 - **Validate Binary Search Tree**:验证一个二叉树是否...
javascript js_leetcode题解之112-path-sum.js
javascript js_leetcode题解之64-minimum-path-sum.js
c语言入门 C语言_leetcode题解之64-minimum-path-sum.c
至于压缩包内的文件`md5sum.exe`,它是这个工具的可执行文件,用户只需将其解压并放在系统的PATH环境变量所包含的任何目录下,或者直接在命令行指定路径调用,即可开始使用。 需要注意的是,尽管MD5在过去的很多年...
Path Sum II **知识点:** - **问题描述:** - 给定一个二叉树和一个目标和,返回所有和为目标值的根节点到叶子节点的路径。 - **解决方案分析:** - **深度优先搜索(DFS):** - 从根节点开始 DFS。 - ...
python python_leetcode题解之124_Binary_Tree_Maximum_Path_Sum
javascript js_leetcode题解之124-binary-tree-maximum-path-sum.js
LeetCode — Path Sum III分析及实现方法 题目描述: 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...
第 338 章力码解决方案 问题编号 标题 困难 C Java Ruby 338 中等的 [✓](/src/338 计数位/solution.c) 104 简单的 [✓](/src/104 ...II/solution.c) ...Path Sum II/solution.java) 258 简单的 [✓](/