[LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)
来源:互联网 发布:压花辊雕刻加工编程 编辑:程序博客网 时间:2024/05/21 19:43
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); }
阅读全文
0 0
- [LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)
- [LeetCode]112. Path Sum(判断二叉树根到叶路径和是否等于sum)
- Tree-----找出和等于给定数字的所有路径(113. Path Sum II)
- [LeetCode]437. Path Sum III(求二叉树中路径和等于sum的数量)
- [Leetcode]113. Path Sum II 求路径和一个数的所有路径
- Path Sum II 二叉树路径和,打印所有路径 @LeetCode
- Sum Root to Leaf Numbers 求所有二叉树根到叶子节点路径之和 @LeetCode
- LeetCode Path Sum II路径和II
- 【leetcode 二叉树路径和】Path Sum 和 Path Sum II
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- Path Sum 二叉树路径和 @LeetCode
- LeetCode 113. Path Sum II(路径和)
- leetcode 113. Path Sum II-路径和|回溯算法
- Leetcode 113. Path Sum II 路径和2 解题报告
- 寻找树根到叶子的权值之和为sum的路径 Path Sum
- LeetCode Path Sum 路径和
- [LeetCode] 路径和 Path Sum
- leetcode Path Sum 路径和
- ROC评估曲线的坑
- android H5 交互 加载网页时替换某个资源
- Eva's Balance-OJ
- git--从远程库克隆
- 程序员必备工具
- [LeetCode]113. Path Sum II(列出二叉树根到叶路径和等于sum的所有路径)
- 什么是字符集
- 最长公共子序列Lcs
- 利用IO流文实现对件夹的拷贝(文本)
- Wireless Network
- 1053. Path of Equal Weight (30)
- leetcode -- 434. Number of Segments in a String 【字符串处理】
- TCP/IP 详解卷一学习笔记(六):TCP 超时与重传
- 产品经理必知的10种数据分析方法