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() {}
相关推荐
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^6 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿8 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉9 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing9 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财9 小时前
加油站(力扣134)
算法·leetcode·职场和发展
王老师青少年编程9 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛