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() {}
相关推荐
点云SLAM11 分钟前
PyTorch 中.backward() 详解使用
人工智能·pytorch·python·深度学习·算法·机器学习·机器人
only-qi40 分钟前
146. LRU 缓存
java·算法·缓存
梁辰兴2 小时前
数据结构:排序
数据结构·算法·排序算法·c·插入排序·排序·交换排序
Lris-KK2 小时前
【Leetcode】高频SQL基础题--1731.每位经理的下属员工数量
sql·leetcode
野犬寒鸦2 小时前
力扣hot100:搜索二维矩阵 II(常见误区与高效解法详解)(240)
java·数据结构·算法·leetcode·面试
菜鸟得菜2 小时前
leecode kadane算法 解决数组中子数组的最大和,以及环形数组连续子数组的最大和问题
数据结构·算法·leetcode
朝阳5813 小时前
用 Rust + Actix-Web 打造“Hello, WebSocket!”——从握手到回声,只需 50 行代码
前端·websocket·rust
楼田莉子3 小时前
C++算法专题学习——分治
数据结构·c++·学习·算法·leetcode·排序算法
一支鱼4 小时前
leetcode常用解题方案总结
前端·算法·leetcode
ulias2124 小时前
各种背包问题简述
数据结构·c++·算法·动态规划