[LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)
2017-07-21 18:53
501 查看
113. Path Sum II
原题链接相似题目题解:112. Path Sum && 437. Path Sum III
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, 列出二叉树中从根节点到叶子节点的路径和等于sum的所有路径
思路:
运用深度优先遍历,结合112. Path Sum的题目,用二维数组paths存储所有符合的路径,用temp存储每次遍历的路径,符合条件就存进paths中
代码1:(16ms)
#include <iostream> #include <vector> #include <queue> using namespace std; //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) {//16ms vector<vector<int>> paths; //存储所有满足条件的路径 vector<int> temp;//存储当前遍历路径 help(root, paths, temp, sum); return paths; } void help(TreeNode* &root, vector<vector<int>> &paths, vector<int> temp, int sum) { if(root == nullptr) return ; temp.push_back(root->val); if(root->left==nullptr && root->right==nullptr && sum==root->val) paths.push_back(temp); if(root->left != nullptr) help(root->left, paths, temp, sum-root->val); if(root->right != nullptr) help(root->right, paths, temp, sum-root->val); } }; // 创建二叉树 TreeNode* CreateTreeByLevel(vector<int> num){ int len = num.size(); if(len == 0){ return NULL; }//if queue<TreeNode*> queue; int index = 0; // 创建根节点 TreeNode *root = new TreeNode(num[index++]); // 入队列 queue.push(root); TreeNode *p = NULL; while(!queue.empty() && index < len){ // 出队列 p = queue.front(); queue.pop(); // 左节点 if(index < len && num[index] != -1){ // 如果不空创建一个节点 TreeNode *leftNode = new TreeNode(num[index]); p->left = leftNode; queue.push(leftNode); } index++; // 右节点 if(index < len && num[index] != -1){ // 如果不空创建一个节点 TreeNode *rightNode = new TreeNode(num[index]); p->right = rightNode; queue.push(rightNode); } index++; }//while return root; } int main() { Solution s; // -1代表NULL vector<int> num = {5,4,8,11,-1,13,4,7,2,-1,-1,5,1}; TreeNode* root = CreateTreeByLevel(num); vector<vector<int> > paths = s.pathSum(root,22); for(int i = 0;i < paths.size();i++){ for(int j = 0;j < paths[i].size();j++){ cout<<paths[i][j]<<" "; } cout<<endl; } return 0; }
代码2:(9ms)
发现更好的解法,每次遍历完左树及时删除元素效率更高
需要注意end函数用法
end函数:
函数原型:
iterator end();
const_iterator end();
功能:
返回一个当前vector容器中末尾元素的迭代器。
vector<vector<int>> pathSum1(TreeNode* root, int sum) {//9ms vector<vector<int>> paths; //存储所有满足条件的路径 vector<int> temp;//存储当前遍历路径 help(root, paths, temp, sum); return paths; } void help1(TreeNode* &root, vector<vector<int>> &paths, vector<int> &temp, int sum) { if(root == nullptr) return ; temp.push_back(root->val); if(root->left==nullptr && root->right==nullptr && sum==root->val) paths.push_back(temp); if(root->left != nullptr) help1(root->left, paths, temp, sum-root->val); if(root->right != nullptr) help1(root->right, paths, temp, sum-root->val); //注意temp.end()指向的是最后一个元素的下一个位置,所以访问最后一个元素的正确操作为:temp.end() -1; //temp.erase() 删除某个元素 temp.erase(temp.end() - 1); }
相关文章推荐
- Tree-----找出和等于给定数字的所有路径(113. Path Sum II)
- 113. Path Sum II(求等于某个数的所有路径)
- leetcode 113. Path Sum II-路径和|回溯算法
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
- LeetCode 113. Path Sum II(路径和)
- [LeetCode]112. Path Sum(判断二叉树根到叶路径和是否等于sum)
- [LeetCode] 113. Path Sum II 路径和 II
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- leetcode 113. Path Sum II (路径和) 解题思路和方法
- Leetcode 113. Path Sum II 路径和2 解题报告
- 找出二叉树中和为k的所有路径 Path Sum II
- Leetcode 113. Path Sum II
- LeetCode113. Path Sum II
- [Leetcode] 113. Path Sum II 解题报告
- [leetcode]113. Path Sum II (medium)
- [leetcode]113. Path Sum II -- JavaScript 代码
- [leetcode] 113. Path Sum II
- Leetcode 113. Path Sum II
- LeetCode 113. Path Sum II
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】