在二元树中找出和为某一值的所有路径(树)----面试题系列
2014-05-01 16:22
344 查看
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数 22 和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12 和 10, 5, 7。
这个题要分两步去完成,先构建树,然后去遍历,边遍历边将经过的点加起来,就用先序遍历这个方法了
#include<iostream>
using namespace std;
struct Tree{
int Data;
Tree *Left, *Right;
Tree(){
Left = Right = NULL;
}
};
int a[10],visit[10];
Tree* Insert(Tree *This,int num){
if (This == NULL){
This = new Tree;
if (This == NULL){
return NULL;
}
else {
This->Data = num;
This->Left = This->Right = NULL;
}
}
if (This->Data > num){
This->Left = Insert(This->Left,num);
}
if (This->Data < num){
This->Right = Insert(This->Right,num);
}
return This;
}
void Print(Tree *node){
if (node){
Print(node->Left);
cout << node->Data << " ";
Print(node->Right);
}
}
void dfs(Tree *tree, int resual,int cont, int sum){
if (sum == resual){
for (int i = 0; i < cont; i++){
cout << visit[i] << " ";
}
cout << endl;
return;
}
if (tree){
visit[cont] = tree->Data;
dfs(tree->Left, resual + tree->Data, cont + 1, sum);
dfs(tree->Right, resual + tree->Data, cont + 1, sum);
}
}
int main(){
Tree *tree=NULL;
for (int i = 0; i < 5; i++){
int num; cin >> num;
tree=Insert(tree,num);
}
Print(tree);
int sum; cin >> sum;
dfs(tree, 0, 0, sum);
system("pause");
}
输出:
10 5 7
10 5 7
10 12
10 12
每种情况输出了两变,why
当我们先序遍历时,到达叶子节点,tree->left跟tree->right 都为NULL,而求和加的是当前节点的值tree->date,所以左右都会再次dfs,所以有两次输出
改正:
#include<iostream>
using namespace std;
struct Tree{
int Data;
Tree *Left, *Right;
Tree(){
Left = Right = NULL;
}
};
int a[10],visit[10];
Tree* Insert(Tree *This,int num){
if (This == NULL){
This = new Tree;
if (This == NULL){
return NULL;
}
else {
This->Data = num;
This->Left = This->Right = NULL;
}
}
if (This->Data > num){
This->Left = Insert(This->Left,num);
}
if (This->Data < num){
This->Right = Insert(This->Right,num);
}
return This;
}
void Print(Tree *node){
if (node){
Print(node->Left);
cout << node->Data << " ";
Print(node->Right);
}
}
void dfs(Tree *tree, int resual,int cont, int sum){
if (sum == resual){
for (int i = 0; i < cont; i++){
cout << visit[i] << " ";
}
cout << endl;
return;
}
if (!tree) return;
if (tree->Left && tree->Right){
visit[cont] = tree->Data;
dfs(tree->Left, resual + tree->Data, cont+1 , sum);
dfs(tree->Right, resual + tree->Data, cont + 1, sum);
}
else if (!tree->Left && !tree->Right){
visit[cont] = tree->Data;
dfs(NULL, resual + tree->Data, cont + 1, sum);
}
}
int main(){
Tree *tree=NULL;
for (int i = 0; i < 5; i++){
int num; cin >> num;
tree=Insert(tree,num);
}
Print(tree);
int sum; cin >> sum;
dfs(tree, 0, 0, sum);
system("pause");
}
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数 22 和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12 和 10, 5, 7。
这个题要分两步去完成,先构建树,然后去遍历,边遍历边将经过的点加起来,就用先序遍历这个方法了
#include<iostream>
using namespace std;
struct Tree{
int Data;
Tree *Left, *Right;
Tree(){
Left = Right = NULL;
}
};
int a[10],visit[10];
Tree* Insert(Tree *This,int num){
if (This == NULL){
This = new Tree;
if (This == NULL){
return NULL;
}
else {
This->Data = num;
This->Left = This->Right = NULL;
}
}
if (This->Data > num){
This->Left = Insert(This->Left,num);
}
if (This->Data < num){
This->Right = Insert(This->Right,num);
}
return This;
}
void Print(Tree *node){
if (node){
Print(node->Left);
cout << node->Data << " ";
Print(node->Right);
}
}
void dfs(Tree *tree, int resual,int cont, int sum){
if (sum == resual){
for (int i = 0; i < cont; i++){
cout << visit[i] << " ";
}
cout << endl;
return;
}
if (tree){
visit[cont] = tree->Data;
dfs(tree->Left, resual + tree->Data, cont + 1, sum);
dfs(tree->Right, resual + tree->Data, cont + 1, sum);
}
}
int main(){
Tree *tree=NULL;
for (int i = 0; i < 5; i++){
int num; cin >> num;
tree=Insert(tree,num);
}
Print(tree);
int sum; cin >> sum;
dfs(tree, 0, 0, sum);
system("pause");
}
输出:
10 5 7
10 5 7
10 12
10 12
每种情况输出了两变,why
当我们先序遍历时,到达叶子节点,tree->left跟tree->right 都为NULL,而求和加的是当前节点的值tree->date,所以左右都会再次dfs,所以有两次输出
改正:
#include<iostream>
using namespace std;
struct Tree{
int Data;
Tree *Left, *Right;
Tree(){
Left = Right = NULL;
}
};
int a[10],visit[10];
Tree* Insert(Tree *This,int num){
if (This == NULL){
This = new Tree;
if (This == NULL){
return NULL;
}
else {
This->Data = num;
This->Left = This->Right = NULL;
}
}
if (This->Data > num){
This->Left = Insert(This->Left,num);
}
if (This->Data < num){
This->Right = Insert(This->Right,num);
}
return This;
}
void Print(Tree *node){
if (node){
Print(node->Left);
cout << node->Data << " ";
Print(node->Right);
}
}
void dfs(Tree *tree, int resual,int cont, int sum){
if (sum == resual){
for (int i = 0; i < cont; i++){
cout << visit[i] << " ";
}
cout << endl;
return;
}
if (!tree) return;
if (tree->Left && tree->Right){
visit[cont] = tree->Data;
dfs(tree->Left, resual + tree->Data, cont+1 , sum);
dfs(tree->Right, resual + tree->Data, cont + 1, sum);
}
else if (!tree->Left && !tree->Right){
visit[cont] = tree->Data;
dfs(NULL, resual + tree->Data, cont + 1, sum);
}
}
int main(){
Tree *tree=NULL;
for (int i = 0; i < 5; i++){
int num; cin >> num;
tree=Insert(tree,num);
}
Print(tree);
int sum; cin >> sum;
dfs(tree, 0, 0, sum);
system("pause");
}
相关文章推荐
- 微软面试题系列(四):在二元树中找出和为某一值的所有路径
- 【面试题】在二元树中找出和为某一值的所有路径
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径
- 4. 微软面试题: 在二元树中找出和为某一值的所有路径(树)
- 算法与数据结构面试题(4)-在二元树中找出和为某一值的所有路径
- 程序员面试题100题第04题——在二元树中找出和为某一值的所有路径
- 数据结构面试题1.2.5-在二元树中找出和为某一值的所有路径
- 【面试题】在二元树中找出和为某一值的所有路径——Java实现
- 面试题06:在二元树中找出和为某一值的所有路径(未完待续)
- 【面试题】在二元树中找出和为某一值的所有路径
- 练习系列 - 6、在二元树中找出和为某一值的所有路径
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径—python实现
- 【面试题】在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- [编程之美-12]在二元树中找出和为某一值的所有路径
- (四)在二元树中找出和为某一值的所有路径
- 每天三道冲刺工作--在二元树中找出和为某一值的所有路径
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径
- 程序员面试题100题第04题——在二元树中找出和为某一值的所有路径
- No4、在二元树中找出和为某一值的所有路径(树)