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() {}
相关推荐
夜晚中的人海18 分钟前
【C++】滑动窗口算法习题
开发语言·c++·算法
violet-lz1 小时前
数据结构四大简单排序算法详解:直接插入排序、选择排序、基数排序和冒泡排序
数据结构·算法·排序算法
·白小白1 小时前
力扣(LeetCode) ——118.杨辉三角(C++)
c++·算法·leetcode
std78791 小时前
Rust 与 Go – 比较以及每个如何满足您的需求
开发语言·golang·rust
CoovallyAIHub1 小时前
超越“识别”:下一代机器视觉如何破解具身智能落地难题?
深度学习·算法·计算机视觉
仰泳的熊猫2 小时前
LeetCode:207. 课程表
数据结构·c++·算法·leetcode
liu****2 小时前
19.map和set的封装
开发语言·数据结构·c++·算法
水冗水孚2 小时前
双指针算法在实际开发中的具体应用之代码Review文章字符串的片段分割
算法·leetcode
DuHz2 小时前
用于汽车雷达应用的步进频率PMCW波形——论文阅读
论文阅读·算法·汽车·信息与通信·信号处理·毫米波雷达
张晓~183399481212 小时前
碰一碰发抖音源码技术搭建部署方案
线性代数·算法·microsoft·矩阵·html5