剑指offer-刷题笔记-中难题-JZ34 二叉树中和为某一值的路径(二)

本文介绍了两种不同的方法来解决剑指Offer中的JZ34问题,即找到二叉树中和为特定值的路径。版本1采用递归方式,通过emplace_back优化路径节点的添加;版本2利用辅助队列实现,同时使用accumulate计算路径总和,当达到目标值时收集结果。这两种方法各有特点,适用于不同的场景。

剑指offer-刷题笔记-中难题-JZ34 二叉树中和为某一值的路径(二)

版本1-递归

注意path.emplace_back(root->val)和path.push_back(root->val)的区别,如果是尾插临时对象的话,push_back()需要先构造临时对象,再将这个对象拷贝到容器的末尾,而emplace_back()则直接在容器的末尾构造对象,这样就省去了拷贝的过程
class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    void dfs(TreeNode* root, int number) {
        // 处理树为空
        if (root == NULL) return;
        // 路径更新
        path.emplace_back(root->val);//注意
        // number更新
        number -= root->val;
        // 如果递归当前节点为叶子节点且该条路径的值已经达到了expectNumber,则更新ret
        if(root->left == NULL && root->right == NULL && number == 0) {
            ret.emplace_back(path);
        }
        // 左右子树递归
        dfs(root->left, number);
        dfs(root->right, number);
        path.pop_back();
    }
    
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        dfs(root, expectNumber);
        return ret;
    }
};

版本2-辅助队列

#include<numeric>
class Solution {
public:
    vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> ret;
        if(root == NULL) return ret;
        pair<vector<int>, TreeNode*> p({root->val}, root);
        // 维护一个path,node的队列
        queue< pair<vector<int>, TreeNode*> > q;
        q.push(p);
        // 如果队列非空
        while(!q.empty()) {
            pair<vector<int>, TreeNode*> cur = q.front();
            q.pop();
            TreeNode* node = cur.second;
            // 左子节点path
            if(node->left) {
                vector<int> left = cur.first;
                left.push_back(node->left->val);
                q.push(make_pair(left, node->left));
            }
            // 右子节点path
            if(node->right) {
                vector<int> right = cur.first;
                right.push_back(node->right->val);
                q.push(make_pair(right, node->right));
            }
            // 叶子节点统计
            if(node->left == NULL && node->right == NULL) {
                vector<int> l = cur.first;
                int sum = accumulate(l.begin(), l.end(), 0);
                if(sum == expectNumber) ret.push_back(l);
            } 
        }
        return ret;
    }
};

JZ34目要求我们 在给定的二叉树中找出所有和为指定路径。这道JZ24二叉树中和为某路径的加强版,因为它需要我们找出所有符合要求的路径,而不是从根节点到叶节点的路径。 我们可以采用深度优先遍历(DFS)的方法来解决这个问。具体做法如下: 1. 首先我们需要定义个函数,用于遍历二叉树: ```python def dfs(node, target, path, result): # node表示当前遍历的节点 # target表示剩余的目标 # path表示当前已选择的路径 # result表示符合要求的路径列表 # 如果遍历到了空节点,直接返回 if not node: return # 把当前节点添加到路径中 path.append(node.val) # 计算当前剩余的目标 target -= node.val # 如果目标为0,说明找到了条符合要求的路径,将其加入结果列表中 if target == 0 and not node.left and not node.right: result.append(path[:]) # 继续递归遍历左右子树 dfs(node.left, target, path, result) dfs(node.right, target, path, result) # 回溯,将刚刚添加的节点从路径中删除 path.pop() ``` 2. 然后我们需要遍历整个二叉树,对于每个遍历到的节点,都调用次`dfs`函数,将其作为先前路径中的部分,并且更新`target`的: ```python def find_path(root, target): result = [] dfs(root, target, [], result) return result ``` 这样,我们就可以得到所有符合要求的路径了。 总之,这道需要我们具备二叉树基础知识和DFS基础知识,我们需要仔细阅读目并思考,再将自己的思路转化为代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值