leetcode简单题25 N.112 路径总和 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() {}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值