(M)DFS:113. Path Sum II
来源:互联网 发布:php会员系统源码 编辑:程序博客网 时间:2024/06/06 10:56
这个题是给出一个二叉树,再给一个目标值target,求出所有从根到叶,并且这个路径的和等于这个目标值的所有路径。
/** * 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) { vector<vector<int>> res; vector<int> path; dfs(root, sum, path, res); return res; } void dfs(TreeNode* root, int sum, vector<int> path, vector<vector<int>>& res) { if(root == NULL) return; if(root->left == NULL && root->right == NULL) { if(sum - root->val == 0) { path.push_back(root->val); res.push_back(path); } else return; } path.push_back(root->val); dfs(root->left, sum - root->val, path, res); dfs(root->right, sum - root->val, path, res); }};
阅读全文
0 0
- (M)DFS:113. Path Sum II
- DFS Path Sum II
- LeetCode 113. Path Sum II DFS求解
- Path Sum & Path Sum II | LeetCode DFS
- 113.leetcode Path Sum II(meidum)[DFS加回溯 ]
- 【LeetCode笔记】113. Path Sum II DFS搜索+记录节点
- leetcode 113. Path Sum II DFS深度优先遍历
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 113. Path Sum II
- 将bitmap转为File文件
- [ Java Zero to One ] 2. Java String 之 高级原理
- EM算法
- /*------------------------------------------------- //目前真正最全的OLE操作Excel的完整代码 //版本:2007.01.15.01 //C+
- 利用python求积分
- (M)DFS:113. Path Sum II
- 不写线段树好多年~~ POJ 3468 A Simple Problem with Integers
- java统计数组中重复元素的个数
- 【BZOJ1207】【HNOI2004】打鼹鼠
- 不要给自己设上限
- 将Url转成File文件
- CXF运行时日志包slf4j报错
- 2016ACM/ICPC亚洲区大连站 J
- 周末总结