【编程题目】在二元树中找出和为某一值的所有路径(树)

4.在二元树中找出和为某一值的所有路径(树)
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数 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
};

 

思路:

直接想到了深度优先搜索,写了一个上午的代码

/*
4.在二元树中找出和为某一值的所有路径(树)
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如  输入整数 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>
using namespace std;

typedef 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
}BinaryTreeNode;

int createBinaryTree(BinaryTreeNode * &T) //创建二叉树
{
    int data;
    if(scanf("%d",&data) != 0 && data != 0) //输入0 表示null
    {
        T = new BinaryTreeNode;
        T->m_nValue = data;
        T->m_pLeft = NULL;
        T->m_pRight = NULL;
        createBinaryTree(T->m_pLeft);
        createBinaryTree(T->m_pRight);
    }
    return 1;
}

int DFSFindSum(BinaryTreeNode * T, int sum)
{
    int ansnum = 0;
    int  ans[20];  //最多20层
    int k = 1;
    vector<BinaryTreeNode *>  S[20];  //最多20层
    S[k - 1].push_back(T);
    while(k >= 1)
    {
        while(!S[k - 1].empty())
        {
            BinaryTreeNode * x = S[k-1].back();
            S[k-1].pop_back();
            ans[k - 1] = x->m_nValue; 
            if(x->m_pLeft != NULL || x->m_pRight != NULL)
            {
                k++;
                if(x->m_pLeft != NULL)
                {
                    S[k-1].push_back(x->m_pLeft);
                }
                if(x->m_pRight != NULL)
                {
                    S[k-1].push_back(x->m_pRight);
                }
            }
            else
            {
                int total = 0;
                for(int i = 0; i < k; i++)
                {
                    total += ans[i];
                }
                if(total == sum)
                {
                    ansnum++;
                    for(int i = 0; i < k; i++)
                    {
                        printf("%d ", ans[i]);
                    }
                    printf("\n");
                }
            }
        }
        if(k >= 1)
        {
            k--;
        }
    }
    if(ansnum == 0)
    {
        printf("no path\n");
    }
    return 0;
}

int main()
{
    BinaryTreeNode * T = NULL;
    createBinaryTree(T);
    DFSFindSum(T, 22);
    return 1;
}

 

后来在网上看了别人写的代码, 发现自己太重视非递归了,其实用递归方便很多

来源:http://blog.csdn.net/yake25/article/details/7375532

#include <iostream>
#include <vector>
using namespace std;

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
};

vector<BinaryTreeNode*> pathVec;

void creatTree(BinaryTreeNode *&root, int *a, int i, int len)
{
    if(i >= len)
        return;
    root = new BinaryTreeNode;
    root->m_nValue = a[i];
    root->m_pLeft = NULL;
    root->m_pRight = NULL;
    creatTree(root->m_pLeft, a, 2*i + 1, len);
    creatTree(root->m_pRight, a, 2*i + 2, len);
}

void preorder(BinaryTreeNode* &root)
{
    if(!root)
        return;
    cout << root->m_nValue << " ";
    preorder(root->m_pLeft);
    preorder(root->m_pRight);    
}

void printPath(vector<BinaryTreeNode*>& pathVec)
{
    for(vector<BinaryTreeNode*>::iterator it = pathVec.begin(); it != pathVec.end(); it++)
        cout << (*it)->m_nValue << " ";
    cout << endl;
}

void pathTree(BinaryTreeNode* &root, int val)
{
//输出二叉树中路径等于val的所有路径
    static int sum = 0;
    sum += root->m_nValue;
    pathVec.push_back(root);
    if(sum == val && root->m_pLeft == NULL && root->m_pRight == NULL)
        printPath(pathVec);
    if(root->m_pLeft)
        pathTree(root->m_pLeft, val);
    if(root->m_pRight)
        pathTree(root->m_pRight, val);
    sum -= root->m_nValue;
    pathVec.pop_back();
}

int main()
{
    BinaryTreeNode *root = 0;
    int a[] = {10, 5, 12, 7, 8};
    int len = sizeof a / sizeof a[0];
    creatTree(root, a, 0, len);
//    preorder(root);
    pathTree(root, 22);
}

 

转载于:https://www.cnblogs.com/dplearning/p/3890080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值