二叉树路径和调试
来源:互联网 发布:淘宝银饰品店铺介绍 编辑:程序博客网 时间:2024/06/12 00:53
//==========================================定义头部 #include <iostream> #include<vector>using namespace std;struct TreeNode {int data;struct TreeNode *lchild, *rchild;//左右孩子 };TreeNode *T;void CreateBiTree(TreeNode* &T) {//按先序输入二叉树中结点的值(一个字符),空格字符代表空树, //构造二叉树表表示二叉树T。 int num;cin >> num;if (num == -1) T = NULL;//其中getchar()为逐个读入标准库函数 else {T = new TreeNode;//产生新的子树 T->data = num; CreateBiTree(T->lchild);//递归创建左子树 CreateBiTree(T->rchild);//递归创建右子树 }}void PathSum(TreeNode *root, int target, vector<int> &vec1, int &sum, vector<vector<int>>&vec2) {vec1.push_back(root->data);cout << root->data << endl;sum += root->data;if (root->lchild == NULL && root->rchild == NULL && sum == target) {vec2.push_back(vec1);}if (root->lchild != NULL) {PathSum(root->lchild, target, vec1, sum, vec2);}if (root->rchild != NULL) {PathSum(root->rchild, target, vec1, sum, vec2);}sum -= root->data;cout << sum << endl;vec1.pop_back();}void main() {// Write your code hereTreeNode *root;CreateBiTree(root);int target=0, sum = 0;vector<vector<int>>vec2;vector<int>vec1;PathSum(root, target, vec1, sum,vec2);}
阅读全文
0 0
- 二叉树路径和调试
- 二叉树路径和
- 二叉树路径和
- 二叉树路径和问题
- 二叉树最大路径和
- 二叉树最大路径和
- 浅说二叉树路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 模拟退火算法的 C++ 实现
- unison+inotify实现web数据双向同步
- 算法导论习题自做2.1-3
- 安卓-setContentView(R.layout.main)意义和作用
- 数据结构 找树根和孩子(树)
- 二叉树路径和调试
- ubuntu使用-软件篇:matlab
- Hudson持续集成服务器的安装配置与使用
- apache 运行库注解(一内存池管理)
- lintcode:最长上升连续子序列
- PAT-A 1008. Elevator (20)
- Shader相关Mask裁切UI粒子特效或者3D模型
- git命令图
- 语义网简介(三)—RDF与RDF Schema