3.在二元树中找出和为某一值的所有路径
来源:互联网 发布:萧何月下追韩信淘宝 编辑:程序博客网 时间:2024/06/08 03:09
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如输入整数22 和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12 和10, 5, 7。
二元树节点的数据结构定义为:
struct BinaryTreeNode // a node in the binary tree
{
int m_nValue; // value of node
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如输入整数22 和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12 和10, 5, 7。
二元树节点的数据结构定义为:
struct BinaryTreeNode // a node in the binary tree
{
int m_nValue; // value of node
BinaryTreeNode *m_pLeft; // left child of node
BinaryTreeNode *m_pRight; // right child of node
};
分析:首先明白题意,就是让我们求出所有满足要求的路径,路径上所有节点的值的和为我们输入的值。
代码实现:
#include<iostream>#include<vector>#include<string>using namespace std;struct TreeNode{int value;struct TreeNode *left;struct TreeNode *right;};vector<int> path;int x;void addTreeNode(struct TreeNode *&pCurrent,int value)//创建二叉排序树{struct TreeNode *temp;if(pCurrent==NULL){temp=new TreeNode();temp->value=value;temp->left=NULL;temp->right=NULL;pCurrent=temp;return ;}if(pCurrent->value>value){addTreeNode(pCurrent->left,value);return ;}if(pCurrent->value<value){addTreeNode(pCurrent->right,value);return ;}cout<<"Node repted!!!"<<endl;}void printPath(){for(vector<int>::iterator iter=path.begin(); iter!=path.end();iter++){cout<<*iter<<" ";}cout<<endl;}void helper(struct TreeNode *pCurrent, int sum){if(pCurrent==NULL) return ;if(pCurrent->left==NULL&&pCurrent->right==NULL&&(sum+pCurrent->value)==x){path.push_back(pCurrent->value);printPath();path.pop_back();return ;}if(pCurrent->left!=NULL){path.push_back(pCurrent->value);helper(pCurrent->left,sum+pCurrent->value);path.pop_back();}if(pCurrent->right!=NULL){path.push_back(pCurrent->value);helper(pCurrent->right,sum+pCurrent->value);path.pop_back();}}void inOrderTree(struct TreeNode *pCurrent)//中序遍历,测试用的{if(pCurrent==NULL) return ;if(pCurrent->left!=NULL)inOrderTree(pCurrent->left);cout<<pCurrent->value<<" ";if(pCurrent->right!=NULL)inOrderTree(pCurrent->right);}int main(){struct TreeNode *root=NULL;addTreeNode(root,10);addTreeNode(root,12);addTreeNode(root,5);addTreeNode(root,4);addTreeNode(root,7);//inOrderTree(root);//cout<<endl;scanf("%d",&x);helper(root,0);return 0;}
0 0
- 3.在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径的个人代码
- 在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径(树)
- 4.在二元树中找出和为某一值的所有路径(树)
- No4、在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径(树)
- 4.在二元树中找出和为某一值的所有路径(树)
- 4.在二元树中找出和为某一值的所有路径(树)
- 4.在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径-递归算法
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 算法讨论(三)---在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 算法题23 在二元树中找出和为某一值的所有路径
- Linux系统调优参数知多少?
- 【HEVC学习与研究】9.HEVC视频编码技术
- uCGUI在VC上的开发
- 【HEVC学习与研究】10.HEVC的档次、层与级别
- discuz后台主导航栏菜单中添加新的菜单项的方法
- 3.在二元树中找出和为某一值的所有路径
- 【HEVC学习与研究】11.HEVC参考解码器的设置及参数解析过程
- 简明 Vim 练级攻略
- session的生命周期
- Cocos 2014开发者大会(秋季):刀塔传奇客户端技术分享/《迷你西游》对3D化的探索/变形金刚和Cocos的邂逅
- HEVC算法和体系结构:HEVC概括性介绍
- 微信开发时PHP代码注释格式
- centos 7 u盘安装
- 我的大学四年