113. Path Sum II
来源:互联网 发布:淘宝收藏店铺看不到 编辑:程序博客网 时间:2024/06/05 00:11
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]]
LeetCode AC代码:
/** * 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) { vector<vector<int>> result; vector<int> cur; pathSum(root, sum, cur, result); return result; } void pathSum(TreeNode *root, int sum, vector<int> &cur, vector<vector<int>> &result) { if (root == NULL) return; cur.push_back(root->val); if (root->left == NULL && root->right == NULL) { if (root->val == sum) result.push_back(cur); } pathSum(root->left, sum - root->val, cur, result); pathSum(root->right, sum - root->val, cur, result); cur.pop_back(); }};
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
- Kafka常用命令
- Windows 8常用快捷键使用大全
- Java学习之Iterator(迭代器)的一般用法
- spring boot 传递Date 等实体参数时候报错
- 获取本机MAC地址
- 113. Path Sum II
- SqueezeNet模型参数降低50倍,压缩461倍
- Kd-Tree算法原理和开源实现代码
- 【9-1-5】简单链表,链式存储
- Spring AOP自定义注解 身份验证
- 2016.12.27回顾
- org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- scp 命令
- redis集群问题及解决