leetcode Path Sum II
来源:互联网 发布:国外游戏直播软件 编辑:程序博客网 时间:2024/06/08 00:43
题目:Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
代码
/** * 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 {private: void find(TreeNode *root,int sum,vector<int> &vec,vector<vector<int>> &ans){ if(root==NULL) return; vec.push_back(root->val); sum-=root->val; if(root->left==NULL && root->right==NULL && sum==0 ) ans.push_back(vec); find(root->left,sum,vec,ans); find(root->right,sum,vec,ans); vec.pop_back(); }public: vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> ans; vector<int> vec; find(root,sum,vec,ans); return ans; }};
0 0
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- [LeetCode]Path Sum II
- [Leetcode]Path Sum II
- [leetcode]Path Sum II
- Leetcode: Path Sum II
- LeetCode-Path Sum II
- [leetcode] Path Sum II
- LeetCode - Path Sum II
- [LeetCode] Path Sum II
- Leetcode Path Sum II
- LeetCode | Path Sum II
- 【leetcode】Path Sum II
- [LeetCode] Path Sum II
- Notification的使用(学习笔记)
- unicode字符集的查询网站
- Qt中通过控件在Label标签中显示一张静态图片
- JNI生成.h文件 找不到' '的类文件
- 【NCRE】——c#判断ppt中的艺术字
- leetcode Path Sum II
- Spring Boot自动配置源码
- macpro 装Ubuntu15 虚拟机 字太小的调节方法
- 检测文件中是否存在bom
- XSS攻击测试语句
- LeetCode OJ 系列之1 Two Sum --Python
- Android Studio Tips-倍数提高工作效率的Android Studio奇技
- Oracle视图
- 基于JQUERY的WEB在线流程图设计器GOOFLOW 0.5版