剑指offer:二叉树中和为某一值的路径
来源:互联网 发布:期货数据网 编辑:程序博客网 时间:2024/06/07 06:44
题目描述
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
/*struct TreeNode {int val;struct TreeNode *left;struct TreeNode *right;TreeNode(int x) :val(x), left(NULL), right(NULL) {}};*/class Solution {public: vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { vector<vector<int>> result; if(root == NULL) return result; vector<int> path; dfs( root, 0, result, expectNumber); return result; } vector<int> path; //DFS void dfs(TreeNode* root, int value, vector<vector<int>> &result, int expectNumber) { if(root == NULL) return; value = value + root->val; path.push_back(root->val); if( root->left == NULL && root->right== NULL) { if(value == expectNumber) result.push_back(path); } dfs( root->left, value, result, expectNumber); dfs( root->right, value, result, expectNumber); //回退,删除最后一个元素 path.erase(path.begin() + path.size() - 1); }};
0 0
- 剑指offer:二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer--二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- 【剑指Offer】二叉树中和为某一值的路径
- 剑指Offer--二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- [剑指offer]二叉树中和为某一值的路径
- 《剑指offer》-二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer|二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 剑指Offer:二叉树中和为某一值的路径
- 剑指offer-二叉树中和为某一值的路径
- Spring Task定时任务
- Linux中的文件与目录管理
- PHP微信APP支付,下单,处理异步回调
- magento中产品分页处理
- Linux笔记
- 剑指offer:二叉树中和为某一值的路径
- “jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。”的解决办法。
- C,C++面试题之2
- 使用Eclipse自动创建SSM框架的WEB工程
- leetcode [Min Stack]
- Python科学计算环境推荐——Anaconda
- Arduino教程:MPU6050的数据获取、分析与处理
- Python的Requests库和Beautifulsoup第三方库一些用法及定义
- CI 多文件上传