LeetCode_Path Sum II

时间:2022-09-16 19:16:03
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]
]

  

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void DFS(TreeNode *root, vector<int> &temp, int tempSum)
{
if(root->left == NULL && root -> right== NULL && tempSum == sum) {
result.push_back(temp);
return;
} if(root->left!= NULL ){
temp.push_back(root->left->val);
DFS(root->left, temp,root->left->val + tempSum );
temp.pop_back();
} if(root->right != NULL ){
temp.push_back(root->right->val);
DFS(root->right, temp, root->right->val + tempSum);
temp.pop_back();
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
result.clear();
if(!root) return result;
this->sum = sum;
vector<int> temp;
temp.push_back(root->val);
DFS(root, temp, root->val);
return result;
}
private:
int sum;
vector<vector<int>> result;
};

LeetCode_Path Sum II的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. 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 ...

  3. &lbrack;leetcode&rsqb;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 ...

  4. 【leetcode】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 ...

  5. 32&period; Path Sum &amp&semi;&amp&semi; Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  6. LeetCode&colon; 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 ...

  7. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. &lbrack;LeetCode&rsqb; &num;167&num; Two Sum II &colon; 数组&sol;二分查找&sol;双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  9. leetcode2&Tab; Two Sum II – Input array is sorted

    Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...

随机推荐

  1. jquery back to top 页面底部的返回顶部按钮

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Linux运行级详解

    对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人 ...

  3. 大话JS面向对象之扩展篇 面向对象与面向过程之间的博弈论(OO Vs 过程)------(一个简单的实例引发的沉思)

    一,总体概要 1,笔者浅谈 我是从学习Java编程开始接触OOP(面向对象编程),刚开始使用Java编写程序的时候感觉很别扭(面向对象式编程因为引入了类.对象.实例等概念,非常贴合人类对于世间万物的认 ...

  4. perl实现awk的功能

    perl -nla -F/\t/ -e"...", 其中-n: while(<>){...}-l: chomp-a: autosplit-F: 与a结合的分隔模式-e: ...

  5. Mybatis源码解析(一)(2015年06月11日)

    一.简介 先看看Mybatis的源码结构图,Mybatis3.2.7版本包含的包共计19个,其他版本可能会少. 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为 ...

  6. 【SysML】模块定义图&lpar;BDD&comma; Block Definition Diagram&rpar;

    一.引言 SysML中的模块定义图,英文为 “Block Definition Diagram”,简称BDD,是系统建模过程中最为常见的图之一,BDD是一种结构图,它主要对系统的结构组成以及组成元素间 ...

  7. HDU1792A New Change Problem&lpar;GCD规律推导)

    A New Change Problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. &lbrack;物理学与PDEs&rsqb;第1章第8节 静电场和静磁场 8&period;3 静磁场

    1. 静磁场: 由稳定电流形成的磁场. 2. 此时, Maxwell 方程组为 $$\beex \bea \Div{\bf D}&=\rho_f,\\ \rot {\bf E}&={\ ...

  9. Centos 7创建一个服务

    首先创建服务文件 vim /etc/systemd/system/node.service #内容如下 [Unit] Description=ethereum-go Monitor Daemon Af ...

  10. java 中getDeclaredFields&lpar;&rpar; 与getFields&lpar;&rpar; 的区别

    java 中getDeclaredFields() 与getFields() 的区别 getDeclaredFields()返回Class中所有的字段,包括私有字段.例证: package com.t ...