Problem Path Sum II

Problem Description: Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.

Solution: 递归。

 1 public List<List<Integer>> pathSum(TreeNode root, int sum) {
 2         List<List<Integer>> list = new ArrayList<List<Integer>>();
 3         if (root == null) return list;
 4         List<Integer> tmp = new ArrayList<Integer>();
 5         path(root, sum, list, tmp);
 6         return list;
 7     }
 8     public void path(TreeNode root, int sum, List<List<Integer>> list, List<Integer> tmp) {
 9         if (root == null) return;
10         tmp.add(root.val);
11         int leftsum = sum - root.val;
12
13         if (leftsum == 0 && root.left == null && root.right == null) {
14             list.add(new ArrayList<Integer>(tmp));
15         }
16         path(root.left, leftsum, list, tmp);
17         path(root.right, leftsum, list, tmp);
18
19         tmp.remove(tmp.size() - 1);
20     }

Problem Path Sum II

时间: 2024-11-05 18:43:28

Problem Path Sum II的相关文章

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

【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_113题——Path Sum II(深度优先搜索)

Path Sum II Total Accepted: 41402 Total Submissions: 155034My Submissions Question Solution 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

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 II [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] ] [题意] 判断二叉树中是否存在一条从根到

Path Sum II leetcode java

题目: 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] ] 题解: 这道题除了要判断是否有这样的一个p

[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

Path Sum,Path Sum II

Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy 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

leetcode 刷题之路 66 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] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到