二叉树中,找出和为某值的所有路径

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

分析:

这题并不难。其实就是深搜。

就是可能一开始,觉得函数递归的时候,要传哪些参数下去?要返回那些值??  

当有多个返回值的时候怎么办??

    Trick:  C++ 中可以采用引用,来返回值的功能。


1. 返回值: 多条 paths, 可以将 vector<vector<int> >& result 作为形参;  这样其实 所有的递归函数都共享了这个 vector; 当找到路径时,push_back即可。

2. 需要往下传的参数: sum,   当前的路径;

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<vector<int> > result;
        vector<int> path;
        
        MyPathSum(root, sum, path, result);
        
        return result;
    }
private:
    void MyPathSum(TreeNode *root, int sum, vector<int> &path, vector<vector<int> > &result)
    {
        if(root == NULL) return;
        
        path.push_back(root->val);
        if(root->left == NULL && root->right == NULL){
            if(sum == root->val){
                result.push_back(path);
            }
        }
        
        if(root->left){
            MyPathSum(root->left, sum - root->val, path, result);
        }
        
        if(root->right){
            MyPathSum(root->right, sum - root->val, path, result);
        }
        path.pop_back();
    }
};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现在二叉树找出和为某一值的所有路径的示例代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; void findPath(TreeNode* root, int sum, int* path, int depth) { if (root == NULL) { return; } path[depth] = root->val; int i, curSum = 0; for (i = depth; i >= 0; --i) { curSum += path[i]; if (curSum == sum) { int j; for (j = i; j < depth; ++j) { printf("%d->", path[j]); } printf("%d\n", path[depth]); } } findPath(root->left, sum, path, depth + 1); findPath(root->right, sum, path, depth + 1); } int main() { // 构造二叉树 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = 10; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->val = 5; root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->val = 12; root->left->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->left->val = 4; root->left->right = (TreeNode*)malloc(sizeof(TreeNode)); root->left->right->val = 7; // 在二叉树查找和为22的路径 int path[100]; findPath(root, 22, path, 0); // 释放二叉树空间 free(root->left->left); free(root->left->right); free(root->left); free(root->right); free(root); return 0; } ``` 该示例代码,我们先构造了一棵二叉树,并在其查找和为22的路径。在查找路径的过程,我们使用了深度优先遍历的方法,通过一个数组来保存当前已经走过的路径,每次遍历到一个节点,则将该节点的值加入路径。在加入值之后,我们检查路径是否存在和为目标值的子路径,如果存在,则输出该路径。最后,我们递归遍历该节点的左子树和右子树,以便查找所有可能的路径。 需要注意的是,该示例代码并未考虑二叉树存在负数的情况,如有需要,可以根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值