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就可以.解题代码:
/*** 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 *rt,long long sum,vector<vector<int> > &ans,vector<int> &path){path.push_back(rt->val);if (rt->left == rt->right && rt->left == NULL){if (sum == rt->val)ans.push_back(vector<int>(path.begin(),path.end()));path.erase(path.end()-1);return ;}if (rt->left)dfs(rt->left,sum - rt->val,ans,path);if (rt->right)dfs(rt->right,sum - rt->val,ans,path);path.erase(path.end()-1);}vector<vector<int> > pathSum(TreeNode *root, int sum) {vector<vector<int> > ans ;if (!root)return ans ;vector<int> path;dfs(root,sum,ans,path);return ans ;}
};

转载于:https://www.cnblogs.com/ldxsuanfa/p/10515912.html

LeetCode:Path Sum II相关推荐

  1. LeetCode Path Sum II(dfs或者bfs)

    问题:给出一个树和一个数,求出从根结点到叶子结点路径和等于这个数的所有情况 思路: 1.深度优先搜索,在到达一个深度结点时,判断是否是叶子结点,并且判断和是否等于要求的数.如果满足,说明是满足条件的一 ...

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

  3. functionclass[LeetCode]Path Sum II

    在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获.只有追求 ...

  4. 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 su ...

  5. [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 giv ...

  6. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  7. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  8. LeetCode 113. Path Sum II

    113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...

  9. 113. Path Sum II

    /** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...

最新文章

  1. asp.net 发送邮件代码
  2. win10家庭版gpedit.msc无法打开本地组策略
  3. 【例4-4】最小花费
  4. 《篡权的ss》-linux命令五分钟系列之三十一
  5. [css] 你有没有使用过“形似猫头鹰”(例:`* + *{ ... }`) 的选择器?
  6. 行业巨头争相布局物联网
  7. Atitit.编程语言原理---方法重载的实现与设计 调用方法的原理
  8. CleanMyMac X4.11.1中文正式版 系统优化 垃圾清理 程序卸载工具
  9. Python学习之Python入门知识(一)
  10. hbase java 建表_Java在HBase数据库创建表
  11. 计算机中文件名无法更改原因,电脑系统文件夹名称修改不了怎么办
  12. 学计算机的怎样分析TCGA数据库,TCGA一些数据库
  13. sklearn:OneHotEncoder的简单用法
  14. node.js学习笔记之简洁聊天室
  15. 在中国人群中感染率最高的高危型HPV病毒是HPV16、HPV52和HPV58
  16. Python计算机视觉编程第十章——OpenCV基础知识
  17. 八字取名,你应该要考虑的7个实用建议
  18. 3、【债券策略】基于利差的债券多空策略(2021-12-27修改)
  19. Django计算机毕业设计母婴商品店进出货管理系统python(源码程序+lw+远程部署)
  20. 如何搭建一个http-server本地服务

热门文章

  1. 机器学习:算法模型:决策树
  2. java XML解析防止外部实体注入
  3. BZOJ2005 NOI2010 能量采集 欧拉函数
  4. ACE(Adaptive Communication Environment)介绍
  5. 【IntelliJ IDEA】添加一个新的tomcat,tomcat启动无法访问欢迎页面,空白页,404
  6. touchesEnded不响应
  7. 实现每个点赞用户点击的带属性的字符串
  8. MeasureSpec学习 - 转
  9. C语言:为什么用fprintf(stderr,Error);比printf(Error);更好?
  10. 2021-08-12 画蜡烛线