[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 andsum = 22,
- 5
- / \
- 4 8
- / / \
- 11 13 4
- / \ / \
- 7 2 5 1
return
- [
- [5,4,11,2],
- [5,8,4,5]
- ]
- 题意:给定一数,在树中找出所有路径和等于该数的情况。
方法一:
使用vector向量实现stack的功能,以方便输出指定路径。思想和代码和Path 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:
- vector<vector<int> > pathSum(TreeNode *root, int sum)
- {
- vector<vector<int>> res;
- vector<TreeNode *> vec;
- TreeNode *pre=NULL;
- TreeNode *cur=root;
- int temVal=;
- while(cur|| !vec.empty())
- {
- while(cur)
- {
- vec.push_back(cur);
- temVal+=cur->val;
- cur=cur->left;
- }
- cur=vec.back();
- if(cur->left==NULL&&cur->right==NULL&&temVal==sum)
- { //和Path sum最大的区别
- vector<int> temp;
- for(int i=;i<vec.size();++i)
- temp.push_back(vec[i]->val);
- res.push_back(temp);
- }
- if(cur->right&&cur->right !=pre)
- cur=cur->right;
- else
- {
- vec.pop_back();
- temVal-=cur->val;
- pre=cur;
- cur=NULL;
- }
- }
- return res;
- }
- };
方法二:递归法
- /**
- * Definition for binary tree
- * 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)
- {
- vector<vector<int>> res;
- vector<int> path;
- findPaths(root,sum,res,path);
- return res;
- }
- void findPaths(TreeNode *root,int sum,vector<vector<int>> &res,vector<int> &path)
- {
- if(root==NULL) return;
- path.push_back(root->val);
- if(root->left==NULL&&root->right==NULL&&sum==root->val)
- res.push_back(path);
- findPaths(root->left,sum-root->val,res,path);
- findPaths(root->right,sum-root->val,res,path);
- path.pop_back();
- }
- };
[Leetcode] Path Sum II路径和的更多相关文章
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 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 give ...
- 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 su ...
- [leetcode]Path Sum II @ Python
原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...
- [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 su ...
随机推荐
- 吐血分享:QQ群霸屏技术(初级篇)
QQ群,仿似一个冷宫;But,你真摒弃不起. 某人,坐拥2000多个2000人群,月收入10w+,此类人数少,皆因多年的沉淀,以形成完全的壁垒,难以企及的层次. 流量的分散,QQ群相对比较优质的地带, ...
- kafka概述
kafka概述 Apache Kafka是一个开源 消息 系统,由Scala写成.是由Apache软件基金会开发的一个开源消息系统项目. Kafka最初是由LinkedIn开发,并于2011年初开源. ...
- eclipse 右键没有Build Path
如果Project Explorer右键没有build pathWindow ->show view 选择package explorer 参考https://blog.csdn.net/cod ...
- keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结
1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...
- 嵌入式Linux系统移植(二)——交叉编译工具集
常用工具:readelf.size.nm.strip.strings.objdump.objcopy.addr2line readelf:读可执行文件的elf头 ELF Header: Magic: ...
- 嵌入式框架Zorb Framework搭建六:定时器的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- 关于 spring-aop理解
对于Aop 一直理解很是不到位 谈谈自己理解! Aop : Aspect: 切面 joinpoint 连接点 pointCut 切点 Advice 增强 targert 目标对象 w ...
- 破解PHPStrom 10 and Pycharm
注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...
- linux内存
在Linux的世界中,从大的方面来讲,有两块内存,一块叫做内存空间,Kernel Space,另一块叫做用户空间,即User Space.它们是相互独立的,Kernel对它们的管理方式也完全不同 驱动 ...
- Qt irrlicht(鬼火)3D引擎 摄像机旋转问题
点击打开链接Irrlicht中的摄像有一个函数 setUpVector() if (m_device != 0 ) { core::vector3df rotation(y,x,0.f); m_cam ...