Path Sum II 二叉树求和
来源:互联网 发布:徐老师的淘宝店怎么进 编辑:程序博客网 时间:2024/05/13 22:21
Path Sum II
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]]
/** * Definition for binary tree * 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; if(root==NULL) return res; solve(res,path,root,sum,0); return res; } void solve(vector<vector<int>> &res, vector<int> &path, TreeNode* root,int sum,int cur) { if(root==NULL) return ; cur+=root->val; path.push_back(root->val); if(cur==sum&&root->left==NULL&&root->right==NULL) res.push_back(path); solve(res,path,root->left,sum,cur); solve(res,path,root->right,sum,cur); path.pop_back(); cur-=root->val; return ; }};
0 0
- Path Sum II 二叉树求和
- 【LeetCode】Path Sum II 二叉树递归
- 二叉树遍历113. Path Sum II
- 二叉树系列---path-sum-II
- 【二叉树】113. Path Sum II
- LeetCode 113: Path Sum II 二叉树遍历
- 【leetcode 二叉树路径和】Path Sum 和 Path Sum II
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- leetcode 113. Path Sum II 二叉树求符合和值为sum的链
- leetcode---path-sum-ii---树
- Path Sum路径求和
- 二叉树 maximum path sum.
- 二叉树系列---path-sum
- 【二叉树】112. Path Sum
- Path Sum II 二叉树路径和,打印所有路径 @LeetCode
- 找出二叉树中和为k的所有路径 Path Sum II
- 113.Path Sum II&二叉树中和为某一值的路径
- 5.4.4—二叉树的递归—Path Sum II
- Jmeter
- 【记录】STM32在ucOS中的串口操作(Interrrupt)
- DIV浮动层被OCX控件遮蔽解决方案
- ExpandableListView仿QQ好友列表
- 详解 Android AsyncTask
- Path Sum II 二叉树求和
- 通过函数指针调用函数
- 析构函数为何可以为虚函数?
- 欢迎使用CSDN-markdown编辑器
- CSS List
- [MySQL复制错误]Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: imposs
- 匈牙利命名法
- js解析Json数据
- Win32时间类型FILETIME/SYSTEMTIME/WINDOWSTIME(FILETIME这个名字很奇怪,其实他跟FILE并没有直接关系,只是很多File的API中,都以这个为时间的类型)