二叉树的和为某一值的路径

题目

输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

思路

利用深度优先搜索遍历所有的路径,然后将路径压入vector<vector<int>>中,并返回。

示例

 /* 构造的树
     *                  9
     *                 / \
     *               4    5
     *              / \   /\
     *             1  10 2  3
     *                       \
     *                        6
     */

构造树用前序和中序序列确定
前序:9,4,1,10,5,2,3,6
中序:1,4,10,9,2,5,3,6
输入:
树的根节点 root 以及目标值 23
输出:
一共有 2 条满足条件的路径,分别是:
9 4 10
9 5 3 6

代码

#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x)
            :val(x),left(nullptr),right(nullptr){

    }
};
class Solution {
public:
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> VVTrace;
        vector<int> VTrace;
        // 是有深度优先算法进行查找
        if(root)
            DFS(root,expectNumber,VVTrace,VTrace);
        return VVTrace;
    }
    void DFS(TreeNode* root,int s,vector<vector<int>> &VVTrace,vector<int> &VTrace) {
        // 将当前结点值入数组
        VTrace.push_back(root->val);
        // 题目要求一条路径是“从根叶子到点”,故此处判断当前结点是否为叶子结点
        if(!root->left&&!root->right) {
            // 如果是叶子结点,且当前期望值等于当前结点值则将此路径入栈
            if(s==root->val)
                VVTrace.push_back(VTrace);
        }
        // 如果当前结点的左孩子不为空,则调用递归对左自己进行DFS
        if(root->left)
            DFS(root->left,s-root->val,VVTrace,VTrace);
        // 如果当前结点的右孩子不为空,则调用递归对右自己进行DFS
        if(root->right)
            DFS(root->right,s-root->val,VVTrace,VTrace);
        VTrace.pop_back();
    }

    // 构造一棵树用来测试
    TreeNode* CreateTree(vector<int> pre, vector<int> mid){
        if (!pre.size() || !mid.size())
            return nullptr;
        TreeNode* root;
        root = CreateTree(pre,0,pre.size()-1, mid, 0, mid.size()-1);
        return root;
    }

private:


    // 构造用于测试的树
    TreeNode* CreateTree(vector<int> pre, int pre_start, int pre_end,
            vector<int> mid, int mid_start, int mid_end){
        if (pre_start > pre_end || mid_start > mid_end)
            return nullptr;
       TreeNode* node = new TreeNode(0);
        for (int i = mid_start; i < mid_end+1 ; ++i) {
          if (mid[i] == pre[pre_start]){
              node->val = mid[i];
              node->left = CreateTree(pre,pre_start+1, i-mid_start+pre_start, mid, mid_start,i-1);
              node->right = CreateTree(pre, i-mid_start+pre_start+1, pre_end, mid, i+1, mid_end);
              break;
          }
        }
        return node;
    }
};

int main(){
    /* 构造的树
     *                  9
     *                 / \
     *               4    5
     *              / \   /\
     *             1  10 2  3
     *                       \
     *                        6
     */
    Solution re;
    vector<int> pre = {9,4,1,10,5,2,3,6};
    vector<int> mid = {1,4,10,9,2,5,3,6};
    TreeNode* tree(nullptr);
    tree = re.CreateTree(pre, mid);
    int expect = 23;
    cout << "-------------" << endl;
    cout << "一共有 " << re.FindPath(tree,expect).size() << " 条满足条件的路径,分别是: " << endl;
    vector<vector<int>> result = re.FindPath(tree,expect);
    vector<int> aa = re.FindPath(tree,expect)[0];
for (int i = 0; i < result.size(); ++i) {
    for (int j = 0; j < result[i].size(); ++j) {
       cout << result[i][j] << " ";
    }
    cout << endl;
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值