LintCode 二叉树的路径和
来源:互联网 发布:互联网数据分析师前景 编辑:程序博客网 时间:2024/05/01 14:37
样例
给定一个二叉树,和 目标值 = 5
:
1 / \ 2 4 / \ 2 3
返回:
[ [1, 2, 2], [1, 4]]
解法:DFS,注意路径和的概念,要到叶子节点
AC代码:
class Solution {public: /* * @param root: the root of binary tree * @param target: An integer * @return: all valid paths */ vector<vector<int>> binaryTreePathSum(TreeNode * root, int target) { // write your code here vector<vector<int>> res; if(!root){ return res; } vector<int> a; TreePathSum(root, target, 0, a, res); return res; } void TreePathSum(TreeNode * root, int target,int now, vector<int>& a, vector<vector<int>> &res) { // write your code here now += root->val; a.push_back(root->val); if(now == target){ if(!root->right && !root->left ) res.push_back(a); } if(root->left){ TreePathSum(root->left, target, now, a, res); a.pop_back(); } if(root->right){ TreePathSum(root->right, target, now, a, res); a.pop_back(); } return ; }};
阅读全文
1 0
- Lintcode 二叉树的路径和
- binaryTreePathSum 二叉树的路径和 lintcode
- lintcode,二叉树的路径和
- LintCode | 376. 二叉树的路径和
- LintCode 376-二叉树的路径和
- LintCode 二叉树的路径和
- 二叉树的路径和-LintCode
- LintCode 二叉树的路径和
- lintcode[2] 二叉树的路径和
- LintCode:二叉树的路径和
- [LintCode]376.二叉树的路径和
- LintCode:二叉树的路径和276
- 二叉树的路径和-LintCode
- [Lintcode] #376 二叉树的路径和
- LintCode 二叉树的路径和
- Lintcode 376. 二叉树的路径和
- [LintCode 376] 二叉树的路径和(Python)
- 二、lintcode刷题记录--二叉树的路径和
- 2018网易校招内推--彩色砖块
- Recyclerview布局多条目加载+上下拉
- 属性动画模版
- MVP登录注册
- 侧滑菜单的使用——
- LintCode 二叉树的路径和
- [RE]一个RE题
- easyui 与 java 通信问题
- 初中生如何入门编程
- Spring 中常用的几种注解
- pta Percolate Up and Down
- PADS设置敷铜实心铜和网格铜
- 4
- 第八周项目五C/C++稀疏矩阵的三元组表示的实现及应用