cpp刷题打卡记录24——路径总和 & 路径总和II

路径总和

法一:迭代

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == NULL){
            return false;
        }
        stack<pair<TreeNode*, int>> st;
        st.push(pair<TreeNode*, int>(root, root->val));
        while(!st.empty()){
            pair<TreeNode*, int> node = st.top();
            st.pop();
            if(!node.first->left && !node.first->right && node.second == targetSum){
                return true;
            }
            if(node.first->right){
                st.push(pair<TreeNode*, int>(node.first->right, node.second+node.first->right->val));
            }
            if(node.first->left){
                st.push(pair<TreeNode*, int>(node.first->left, node.second+node.first->left->val));
            }
        }
        return false;
    }
};

思路是:整体采用前序遍历,因为既要遍历树的节点又需要记录从头节点到当前节点的路径的值,所以用pair结构 pair<TreeNode*, int> pair<节点指针, 路径数值> 来存放在这个栈里的元素。还需注意的是路径是根节点到叶子节点的,叶子节点是没有左孩子节点和右孩子节点的。

法二·:递归

cpp 复制代码
class Solution {
public:
    bool traversal(TreeNode* current, int sum){
        if(!current->left && !current->right && sum == 0){
            return true;
        }
        if(!current->left && !current->right){
            return false;
        }

        if(current->left){
            sum -= current->left->val;
            if(traversal(current->left, sum)){
                return true;
            }
            sum += current->left->val;
        }

        if(current->right){
            sum -= current->right->val;
            if(traversal(current->right, sum)){
                return true;
            }
            sum += current->right->val;
        }
        return false;
    }
    
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == NULL){
            return false;
        }
        return traversal(root, targetSum - root->val);
    }
};

及其不擅长写递归。

路径总和II

cpp 复制代码
class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;
    void traversal(TreeNode* current, int count){
        if(!current->left && !current->right && count == 0){
            result.push_back(path);
            return;
        }

        if(!current->left && !current->right){
            return;
        }

        if(current->left){
            path.push_back(current->left->val);
            count -= current->left->val;
            traversal(current->left, count);
            count += current->left->val;
            path.pop_back();
        }

        if(current->right){
            path.push_back(current->right->val);
            count -= current->right->val;
            traversal(current->right, count);
            count += current->right->val;
            path.pop_back();
        }
        return;
    }

    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(root == NULL){
            return result;
        }        
        path.push_back(root->val); //别忘记把根节点放进路径
        traversal(root, targetSum - root->val);
        return result;
    }
};

与上一题不同的是,这道题目需要遍历整个树

!!一定要注意traversal中if里面的return,这个return只是返回了当前的递归,没有结束整个递归。也就是说在找到一条路径后,只结束当前节点的递归,返回到父节点,然后继续探索该父节点的其他子节点。

本集心得:

讨厌递归,老是想不到。

相关推荐
JieE2121 天前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2122 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack202 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树3 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2123 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2123 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术3 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦3 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
clint4564 天前
C++进阶(1)——前景提要
c++