LeetCode(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.
For example:
Given the below binary tree and sum = 22,
分析
本题目与上一题LeetCode(112) Path Sum虽然类型相同,但是需要在以前的基础上,多做处理一些:
与Path Sum相比,本题是求出路径,所以,在找到满足的路径之后,不能直接返回,而是将其添加到一个vector < vector >中。在查找的过程中,每经过一个结点,先使用一个vector将该路径中的所有结点记录下来。由于vector< vector>是用来记录所有满足的路径的,所以传递引用给它是为了对它的每一个改动都是对其本身的操作,而不是对其副本的操作,使用来返回找到的所有路径。使用的vector只是拷贝操作,所以都是对其副本的操作,不会对原始的vector有影响。
AC代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if (!root)
return vector<vector<int>>();
//存储所有满足条件的路径
vector<vector<int> > paths;
//存储当前遍历路径
vector<int> tmp;
getPaths(root, sum, paths, tmp);
return paths;
}
void getPaths(TreeNode *root, int sum, vector<vector<int> > &paths, vector<int> tmp)
{
if (!root)
return;
tmp.push_back(root->val);
if (!root->left && !root->right && sum == root->val)
paths.push_back(tmp);
if (root->left)
getPaths(root->left, sum - root->val, paths, tmp);
if (root->right)
getPaths(root->right, sum - root->val, paths, tmp);
}
};
LeetCode(113) Path Sum II的更多相关文章
- LeetCode(112) Path Sum
题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- [LeetCode] #112 #113 #437 Path Sum Series
首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(113):路径总和 II
Medium! 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树,以及目标和 sum = ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(304)Range Sum Query 2D - Immutable
题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
随机推荐
- CVE-2012-0002(MS12-020)3389远程溢出漏洞
1:攻击机:Kali和windows皆可 2:目标机:windows XP系统(开启3389) Kali测试: search m12-020 搜索ms12-020 use auxiliary/dos/ ...
- [在读] javascript权威指南第六版
耽搁了有大半年没看,记得当时看到5分之2了吧.权威指南是不管读几遍都能觉得有新收获的书^^
- JSP新闻发布系统
1.主页面 1.1登录 1.2 分页 2.注销 3.代码如下 package cn.news.dao.impl; import java.sql.SQLException; import org ...
- ubuntu快捷键收集
Ctrl+Alt+T 终端 -Ctrl+Shift+C 终端复制 -Ctrl+Shift+V 终端粘贴 -Ctrl+L 清屏 -Ctrl+; 从剪切板中获得输入提示(不小心点到被诡异的提示吓到了) - ...
- Java编程基础-选择和循环语句
一.选择结构语句 选择结构:也被称为分支结构.选择结构有特定的语法规则,代码要执行具体的逻辑运算进行判断,逻辑运算的结果有两个,所以产生选择,按照不同的选择执行不同的代码. Java语言提供了两种选择 ...
- 洛谷 P2483 [SDOI2010]魔法猪学院
题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...
- 洛谷 P2617 Dynamic Ranking
题目描述 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤ ...
- 洛谷 1164 小A点菜
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...
- 在Ubuntu16.04安装YouCompleteMe
作为从事了4年多嵌入式Linux工作的软件工程师,最近决定完全在ubuntu上工作,使用vim进行代码的阅读和编辑,然后尝试去安装vim相关的各种插件.从来没用过代码补全的我,在网上找到了插件omni ...
- SublimeREPL配置Python3开发
首先什么是REPL? A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includ ...