113. Path Sum II
来源:互联网 发布:网络连接修复工具 编辑:程序博客网 时间:2024/05/18 03:26
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的全部路径,这里的路径是根节点到叶节点的路径。使用dfs可轻松破之。
代码:
class Solution{public:vector<vector<int>> pathSum(TreeNode* root, int sum){vector<int> path;dfs(root, 0, sum, path);return res;}private:vector<vector<int>> res;void dfs(TreeNode* root, int sum, int target, vector<int> path){if(!root){return;}path.push_back(root->val);sum += root->val;if(!root->left && !root->right){if(sum == target){res.push_back(path);}return;}dfs(root->left, sum, target, path);dfs(root->right, sum, target, path);}};
阅读全文
0 0
- 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
- 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
- Q趣聊天通信软件应用设计
- 搭建Nginx+Java环境
- css --左侧菜单a标签 改变右侧ifame引用的页面
- java内部静态类和非静态类,和他们在外部的定义
- 《JavaScript高级程序设计》学习笔记
- 113. Path Sum II
- wget
- GregorianCalendar的使用
- TensorFlow tf.nn.conv2d
- 联想Y40加装固态硬盘
- 详解Oracle的几种分页查询语句
- 无影无踪的增量(又理解下java的的引用....)
- 453. Minimum Moves to Equal Array Elements移动最小的步数使所有元素相等
- u盘安装ubuntu10.04