经典递归回溯!
package Level2;
import java.util.ArrayList;
import Utility.TreeNode;
/**
* 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 S113 {
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
TreeNode n1 = new TreeNode(4);
TreeNode n2 = new TreeNode(8);
root.left = n1;
root.right = n2;
TreeNode n3 = new TreeNode(11);
TreeNode n4 = new TreeNode(13);
TreeNode n5 = new TreeNode(4);
n1.left = n3;
n2.left = n4;
n2.right = n5;
TreeNode n6 = new TreeNode(7);
TreeNode n7 = new TreeNode(2);
n3.left = n6;
n3.right = n7;
TreeNode n8 = new TreeNode(5);
TreeNode n9 = new TreeNode(1);
n5.left = n8;
n5.right = n9;
ArrayList<ArrayList<Integer>> list = pathSum(root, 22);
System.out.println(list);
}
public static ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> l =new ArrayList<Integer>();
dfs(root, sum, list, l);
return list;
}
// 经典递归回溯
private static void dfs(TreeNode root, int sum, ArrayList<ArrayList<Integer>> list, ArrayList<Integer> l){
if(root == null){
return;
}
// 找到最后一个合适的叶子节点
if(root.val==sum && root.left==null && root.right==null){
l.add(root.val);
// 注意!在把结果加入结果集时,必须深拷贝一份!
// 否则被加进去的集合之后可能会变动
ArrayList<Integer> clone = new ArrayList<Integer>(l);
list.add(clone);
l.remove(l.size()-1); // 恢复全局变量//回溯部分
return;
}
l.add(root.val);
dfs(root.left, sum-root.val, list, l);
dfs(root.right, sum-root.val, list, l);
l.remove(l.size()-1); // 恢复全局变量//回溯部分
}
}
分享到:
相关推荐
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...
2sum leetcode leetcode 文件和描述 removeelement.cpp,删除特定元素 removeup.cpp,从排序数组中删除重复项 removeup2.cpp,从排序数组中删除重复项,重复项最多分配两次 pascaltriangle.cpp, 给定行数生成一个...
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...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
1496.-Path-Crossing-LeetCode 7u7基本原则法规
371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...
雨果-leetcode-仪表板 :sparkles:一个LeetCode答题看板的生成插件,完成一键部署到Hugo站点。完整记录刷题心路历程 :sparkles: 屏幕截图 安装 下载Repo到本地: git clone ...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...
求和二 LeetCode Java问题。 LeetCode问题的解决方案。 如果感兴趣,以下是问题描述的URL: 如果对我的评论和方法感兴趣,可以在我的博客中找到以下文章: 谢谢,享受; 约翰
Leetcode two sum java 解法
leetcode_0001_two_sum.c leetcode_0011_max_area.c leetcode_0015_three_sum.c leetcode_0016_three_sum_closest.c leetcode_0018_four_sum.c : not ready leetcode_0026_remove_duplicates.c 27, 80, 283 ...
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
leetcode 2 leetcode-训练 算法训练。 java $: javac hello.java java $: java hello c $: gcc hello.c 如果没有错误会生成a.out 可执行文件 c $: ./a.out leetcode cli leetcode version leetcode help leetcode ...
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...
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
3sum 力码 力扣算法 (注:“ :locked: " 表示你需要来自 Leetcode) # 标题 解决方案 困难 001 简单的 002 中等的 003 中等的 004 难的 005 中等的 006 简单的 007 简单的 008 简单的 009 简单的 010 难的 011 中等...