【剑指offer-解题系列(24)】二叉树中和为某一值
来源:互联网 发布:淘宝运营规则是什么 编辑:程序博客网 时间:2024/06/09 19:17
题目描述
分析
深度搜索遍历,直到到达叶节点,然后统计出和。
代码实现
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int> >res;
vector<int>path;
int current_num = 0;
getpath( root, expectNumber,res,path,current_num);
return res;
}
void getpath(TreeNode* root,int expectNumber,vector<vector<int>>&res,vector<int> path,
int current_num){
if(root==NULL)
return;
current_num +=root->val;
if(current_num>expectNumber){
return;
}
path.push_back(root->val);
if(root->left==NULL&&root->right==NULL){
if(current_num==expectNumber){
res.push_back(path);
}
}
if(root->left!=NULL){
getpath( root->left, expectNumber,res,path,current_num);
}
if(root->right!=NULL){
getpath( root->right, expectNumber ,res,path,current_num);
}
}
- 【剑指offer-解题系列(24)】二叉树中和为某一值
- 剑指Offer——(24)二叉树中和为某一值的路径
- 剑指offer第24题(二叉树中和为某一值的路径)
- 剑指offer:二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer--二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- 【剑指Offer】二叉树中和为某一值的路径
- 剑指Offer--二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- [剑指offer]二叉树中和为某一值的路径
- 《剑指offer》-二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer|二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- Windows RabbitMQ服务端安装与配置详解
- Java面向对象编程
- 什么是分布式缓存框架以及分布式缓存框架的优缺点
- nodejs(koajs)设置中文cookie无效
- [教程] React Native基础实战(1)—— 制作一个简单的按钮
- 【剑指offer-解题系列(24)】二叉树中和为某一值
- eclipse添加代码提示
- 软件架构模式
- 【剑指offer-解题系列(21)】栈的压入、弹出序列
- 安卓错误:cannot be resolved or is not a field
- eclipse启动时内存溢出的解决办法
- 学习笔记TF015:加载图像、图像格式、图像操作、颜色
- 浅谈MVP架构在Android中的应用
- 李彦宏说数据秒杀算法,BAT金融大数据如何运作?