[数据结构+算法] 给一棵树和一个sum,判断是否存在从root到叶子结点的path之和等于sum?

数据结构+算法 给一棵树和一个sum,判断是否存在从root到叶子结点的path之和等于sum?

可以使用两种方法求解

递归 CheckTreeSumRecursive

问题转换为递归判断左右子树是否满足路径和等于sum减去当前节点的值。

迭代 CheckTreeSumNonRecursive

使用两个栈数据结构,一个存储节点,另一个存储对应的节点到root节点到sum,迭代遍历到叶子节点时进行判断。

详细代码如下:

cpp 复制代码
#include <iostream>
#include <stack>

using namespace std;

struct TreeNode {
    TreeNode(int val_) : val(val_), left(nullptr), right(nullptr) {}

    int val;
    TreeNode *left;
    TreeNode *right;
};

bool CheckTreeSumRecursive(TreeNode *head, int targetSum) {
    if (head == nullptr) {
        return false;
    }

    if (head->left == nullptr && head->right == nullptr && head->val == targetSum) {
        return true;
    }

    return CheckTreeSumRecursive(head->left, targetSum - head->val) || 
            CheckTreeSumRecursive(head->right, targetSum - head->val);
}

bool CheckTreeSumNonRecursive(TreeNode *head, int targetSum) {
    if (head == nullptr) {
        return false;
    }

    stack<TreeNode*> nodes;
    nodes.push(head);
    stack<int> sums;
    sums.push(head->val);

    while (!nodes.empty()) {
        TreeNode *node = nodes.top();
        nodes.pop();
        int sum = sums.top();
        sums.pop();
        if (node->left == nullptr && node->right == nullptr && sum == targetSum) {
            return true;
        }
        if (node->left != nullptr) {
            nodes.push(node->left);
            sums.push(sum + node->val);
        }

        if (node->right != nullptr) {
            nodes.push(node->right);
            sums.push(sum + node->val);
        }
    }
    
    return false;
}

// 打印结果的辅助函数
void printResult(bool result) {
    cout << (result ? "true" : "false") << endl;
}

int main() {
    // 创建示例二叉树
    TreeNode* root = new TreeNode(5);
    root->left = new TreeNode(4);
    root->right = new TreeNode(8);
    root->left->left = new TreeNode(11);
    root->left->left->left = new TreeNode(7);
    root->left->left->right = new TreeNode(2);
    root->right->left = new TreeNode(13);
    root->right->right = new TreeNode(4);
    root->right->right->right = new TreeNode(1);
    
    cout << "Test Recursive Solution...\n";
    cout << "Example 1: ";
    printResult(CheckTreeSumRecursive(root, 22)); // 输出 true
    cout << "Example 2: ";
    printResult(CheckTreeSumRecursive(root, 5)); // 输出 false
    cout << "Example 3: ";
    printResult(CheckTreeSumRecursive(nullptr, 0)); // 输出 false

    cout << "Test Recursive Solution...\n";
    cout << "Example 1: ";
    printResult(CheckTreeSumNonRecursive(root, 22)); // 输出 true
    cout << "Example 2: ";
    printResult(CheckTreeSumNonRecursive(root, 5)); // 输出 false
    cout << "Example 3: ";
    printResult(CheckTreeSumNonRecursive(nullptr, 0)); // 输出 false

    return 0;
}
相关推荐
一次旅行27 分钟前
AutoAWQ完整实战:MIT激活感知AWQ量化,模型显存减半、推理提速且精度无损
人工智能·python·算法
GrowthDiary0071 小时前
算法题:寻找二维数组top k问题
数据结构·python·算法
可编程芯片开发1 小时前
基于全阶观测器的三自由度运动系统状态反馈控制simulink建模与仿真
算法
kobesdu1 小时前
从零推导FAST-LIO的观测雅可比矩阵
人工智能·算法·矩阵
SHARK_pssm1 小时前
【数据结构——栈】
数据结构
乐思智能科技有限公司1 小时前
PLECS软件学习使用(二)直流电机基本系统模型
人工智能·算法·机器学习·面试·职场和发展
阿米亚波1 小时前
【C++ STL】std::unordered_multiset
开发语言·数据结构·c++·笔记·stl
随意起个昵称2 小时前
贪心模型-Johnson法则
c++·算法
蜡笔小马2 小时前
【保姆级教程】Windows 下 CGAL 6.2 + VS2022 全流程实战:从源码编译到项目集成(附避坑指南)
c++·cgal
xiaoye-duck2 小时前
《Linux系统编程》Linux 系统多线程(八): C++ 高并发线程池全链路深度解析与从零手撕实现
linux·c++·线程池