leetcode:Path Sum II

又一道DFS题,题意如下:

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

非常容易,跟之前的打印所有路径是一样的,直接上代码:

static List<List<Integer>> result;
	static List<Integer> current;
	public static List<List<Integer>> pathSum(TreeNode root, int sum) {
        result = new ArrayList<List<Integer>>();
        current = new ArrayList<Integer>();
        s(root,sum);
        return result;
    }

	static void s(TreeNode root,int sum){

		if(root == null){
			return;
		}else if(root.left == null && root.right == null){
			current.add(root.val);
			int temp = 0;
			for(int i=0;i<current.size();i++)
			{
				temp+=(int)current.get(i);
			}
			if(temp == sum){
				List<Integer> ct = new ArrayList<Integer>(current);
				result.add(ct);
			}
			current.remove(current.size()-1);
			return ;
		}
		current.add(root.val);
		s(root.left,sum);
		s(root.right,sum);
		current.remove(current.size()-1);
		return;
	}

不过也有一个小细节需要注意,就是在给result做add方法的时候,不可以用静态变量的引用去添加,只要没有new一个新的对象,所有的引用都指向原来的一份拷贝,所以一旦修改,result里的也会变,所以需要每一次都new一个新的一维list去添加到result。

时间: 2024-08-13 18:18:02

leetcode:Path Sum II的相关文章

【Leetcode】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] ] 思路:与[Leetcode]Path Sum 不同

[Leetcode Week14]Path Sum II

Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Example Given the below binary tree and sum = 22 5

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文: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. 中文:给定一颗二叉树,如

[LeetCode][JavaScript]Path Sum II

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] ] https://leetco

leetcode笔记: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] ] 二.题目分析 这道题与Path Su

LeetCode 113. Path Sum II 20170705 部分之前做了没写的题目

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] ] 题目大意:给定一棵二叉树和一个总和,找出所有从根节点

[LeetCode][Java] 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] ] 题意: 给定一棵二叉树和一个和,找出从根节

[C++]LeetCode: 91 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] ] 思路:构造一个辅助函数.辅助函数是一个深度

LeetCode 113. Path Sum II路径总和 II (C++)

题目: 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 and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 Return: [ [5,4,11