leetcode简单题25 N.112 路径总和 rust描述

rust 复制代码
use std::rc::Rc;
use std::cell::RefCell;

// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
    pub val: i32,
    pub left: Option<Rc<RefCell<TreeNode>>>,
    pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
    #[inline]
    pub fn new(val: i32) -> Self {
        TreeNode {
            val,
            left: None,
            right: None,
        }
    }
}
// 递归dfs
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
    fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
        if let Some(node) = node {
            let node = node.borrow();
            let remaining_sum = target_sum - node.val;
            if node.left.is_none() && node.right.is_none() {
                return remaining_sum == 0;
            }
            dfs(&node.left, remaining_sum) || dfs(&node.right, remaining_sum)
        } else {
            false
        }
    }

    dfs(&root, target_sum)
}
// 迭代dfs
pub fn has_path_sum2(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
    if root.is_none() {
        return false;
    }

    let mut stack = vec![(root.clone(), target_sum)];

    while let Some((node, remaining_sum)) = stack.pop() {
        if let Some(node) = node {
            let node = node.borrow();
            let new_sum = remaining_sum - node.val;

            if node.left.is_none() && node.right.is_none() && new_sum == 0 {
                return true;
            }

            if let Some(ref left) = node.left {
                stack.push((Some(Rc::clone(left)), new_sum));
            }

            if let Some(ref right) = node.right {
                stack.push((Some(Rc::clone(right)), new_sum));
            }
        }
    }

    false
}
use std::collections::VecDeque;
// bfs
pub fn has_path_sum3(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
    if root.is_none() {
        return false;
    }

    let mut queue = VecDeque::new();
    queue.push_back((root.clone(), target_sum));

    while let Some((node, remaining_sum)) = queue.pop_front() {
        if let Some(node) = node {
            let node = node.borrow();
            let new_sum = remaining_sum - node.val;

            if node.left.is_none() && node.right.is_none() && new_sum == 0 {
                return true;
            }

            if let Some(ref left) = node.left {
                queue.push_back((Some(Rc::clone(left)), new_sum));
            }

            if let Some(ref right) = node.right {
                queue.push_back((Some(Rc::clone(right)), new_sum));
            }
        }
    }

    false
}
fn main() {}
相关推荐
wabs6667 小时前
关于图论【卡码网117.软件构建的思考】
数据结构·算法·软件构建·图论·卡码网
mifengxing8 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
魔镜前的帅比8 小时前
(开源项目)x-claw(总)
python·ai·rust·开源
Tongzhi20268 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
Wang's Blog8 小时前
AI Agent白手起家30: 动态示例选择器之根据长度选择 Few Shot 示例
算法
oyguyteggytrrwwwrt10 小时前
自制交叉线路识别算法
算法
手写码匠10 小时前
华为云Flexus+DeepSeek征文|Dify 多智能体协同编排实战:R1 规划 + V3 执行,构建企业 Agent 团队
人工智能·深度学习·算法·aigc
晓天衡宇•评测社区11 小时前
FSR-Bench 前沿科学推理榜单发布:GPT-5.5 居首,“会推理”未必“能答对”
算法
程序喵大人12 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
Source.Liu12 小时前
【Uppsala】入口模块(lib.rs)
xml·rust