Path SumII|leetcode题解
来源:互联网 发布:微信聊天数据迁移 编辑:程序博客网 时间:2024/06/08 20:05
这题使用树的先序遍历,同时存储路径就可以解决。
void preOrder(vector<vector<int> >&paths,TreeNode *root, int sum, vector<int>&path,int level){ if(NULL==root->left&&NULL==root->right){ if(sum==root->val){ path.push_back(root->val); paths.push_back(path); } return; } //if(sum<=root->val)return; sum-=root->val; path.push_back(root->val); if(root->left)preOrder(paths,root->left,sum,path,level+1); path.resize(level);//访问由子树前,恢复path状态 if(root->right)preOrder(paths,root->right,sum,path,level+1); } vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int> >paths; if(NULL==root) return paths; vector<int>path; preOrder(paths,root,sum,path,1); return paths; }写递归函数时,要注意递归函数的出口和调用递归函数时的状态。
0 0
- Path SumII|leetcode题解
- LeetCode 113 Path SumII
- Leetcode dfs Path SumII
- path sumII
- Leetcode dfs Combination SumII
- leetcode c++ combination sumII
- LeetCode 040 Combination SumII
- Leetcode之Combination SumII
- 167.[LeetCode]Two SumII
- leetcode(E)two sumII
- LeetCode题解:Simplify Path
- LeetCode题解:Path Sum
- LeetCode题解:Path Sum
- LeetCode题解:Path Sum
- LeetCode题解:Minimum Path Sum
- LeetCode 题解(219) : Path Sum
- LeetCode题解:Path Sum II
- Leetcode题解 112. Path Sum
- 软件工程师之一:奠定基础
- python学习——基础
- iOS应用性能调优的25个
- Struts2 Result详解
- T-kernel 版本确认
- Path SumII|leetcode题解
- 4.Java NIO系列教程之Scatter/Gather
- java错误,找不到标示符
- 3-3 uva 1225 - Digit Counting
- Linux与JVM的内存关系分析
- Liferay6.2开发之弹出框
- 记录C语言的回忆-C语言种的面向思想
- PL/ SQL 触发器
- 水仙花数——升级版