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



     

    1. use std::rc::Rc;
    2. use std::cell::RefCell;
    3. // Definition for a binary tree node.
    4. #[derive(Debug, PartialEq, Eq)]
    5. pub struct TreeNode {
    6. pub val: i32,
    7. pub left: Option>>,
    8. pub right: Option>>,
    9. }
    10. impl TreeNode {
    11. #[inline]
    12. pub fn new(val: i32) -> Self {
    13. TreeNode {
    14. val,
    15. left: None,
    16. right: None,
    17. }
    18. }
    19. }
    20. // 递归dfs
    21. pub fn has_path_sum(root: Option>>, target_sum: i32) -> bool {
    22. fn dfs(node: &Option>>, target_sum: i32) -> bool {
    23. if let Some(node) = node {
    24. let node = node.borrow();
    25. let remaining_sum = target_sum - node.val;
    26. if node.left.is_none() && node.right.is_none() {
    27. return remaining_sum == 0;
    28. }
    29. dfs(&node.left, remaining_sum) || dfs(&node.right, remaining_sum)
    30. } else {
    31. false
    32. }
    33. }
    34. dfs(&root, target_sum)
    35. }
    36. // 迭代dfs
    37. pub fn has_path_sum2(root: Option>>, target_sum: i32) -> bool {
    38. if root.is_none() {
    39. return false;
    40. }
    41. let mut stack = vec![(root.clone(), target_sum)];
    42. while let Some((node, remaining_sum)) = stack.pop() {
    43. if let Some(node) = node {
    44. let node = node.borrow();
    45. let new_sum = remaining_sum - node.val;
    46. if node.left.is_none() && node.right.is_none() && new_sum == 0 {
    47. return true;
    48. }
    49. if let Some(ref left) = node.left {
    50. stack.push((Some(Rc::clone(left)), new_sum));
    51. }
    52. if let Some(ref right) = node.right {
    53. stack.push((Some(Rc::clone(right)), new_sum));
    54. }
    55. }
    56. }
    57. false
    58. }
    59. use std::collections::VecDeque;
    60. // bfs
    61. pub fn has_path_sum3(root: Option>>, target_sum: i32) -> bool {
    62. if root.is_none() {
    63. return false;
    64. }
    65. let mut queue = VecDeque::new();
    66. queue.push_back((root.clone(), target_sum));
    67. while let Some((node, remaining_sum)) = queue.pop_front() {
    68. if let Some(node) = node {
    69. let node = node.borrow();
    70. let new_sum = remaining_sum - node.val;
    71. if node.left.is_none() && node.right.is_none() && new_sum == 0 {
    72. return true;
    73. }
    74. if let Some(ref left) = node.left {
    75. queue.push_back((Some(Rc::clone(left)), new_sum));
    76. }
    77. if let Some(ref right) = node.right {
    78. queue.push_back((Some(Rc::clone(right)), new_sum));
    79. }
    80. }
    81. }
    82. false
    83. }
    84. fn main() {}

  • 相关阅读:
    鸿蒙文件操作事前准备
    机器学习算法-集成学习
    操作系统MIT6.S081:P8->Multiprocessors and locking
    @Repository详解
    Python爬虫库urllib使用详解
    Java键盘录入案例
    电脑硬盘分区表的两种格式:MBR 和 GPT
    分布式事务。seata主线版本1.6.0-SHAPSHOT,Springboot2.7.6,AT与TCC模式。小白入门必看,0-1过程,代码全。
    window 7 安装VC-redist.x64.exe的问题
    【Hive】快速入门~
  • 原文地址:https://blog.csdn.net/BECOMEviolet/article/details/140452162