1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//! Maximum Depth of Binary Tree[leetcode: maximum_depth_of_binary_tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) //! //! Given a binary tree, find its maximum depth. //! //! The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. //! //! **Note:** A leaf is a node with no children. //! //! **example:** //! Given binary tree `[3,9,20,null,null,15,7]`, //! //! ``` //! 3 //! / \ //! 9 20 //! / \ //! 15 7 //! ``` //! return its depth = 3. use std::rc::Rc; use std::cell::RefCell; use std::cmp::Ord; /// # Solutions /// /// # Approach 1: DFS /// /// * Time complexity: O(n) /// /// * Space complexity: O(1) /// /// * Runtime: 4 ms /// /// * Memory: 3.1 MB /// /// ```rust /// // 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 /// // } /// // } /// // } /// use std::rc::Rc; /// use std::cell::RefCell; /// use std::cmp::Ord; /// impl Solution { /// pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { /// match root { /// Some(node) => { /// let left = Self::max_depth(node.borrow().left.clone()); /// let right = Self::max_depth(node.borrow().right.clone()); /// /// 1 + left.max(right) /// }, /// _ => 0, /// } /// } /// } /// ``` /// /// # Approach 2: BFS /// /// * Time complexity: O(n) /// /// * Space complexity: O(1) /// /// * Runtime: 0 ms /// /// * Memory: 3.2 MB /// /// ```rust /// // 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 /// // } /// // } /// // } /// use std::rc::Rc; /// use std::cell::RefCell; /// use std::collections::VecDeque; /// impl Solution { /// pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { /// if root.is_none() { return 0; } /// /// let mut depth = 0; /// let mut deque: VecDeque<Option<Rc<RefCell<TreeNode>>>> = VecDeque::new(); /// deque.push_back(root); /// /// while !deque.is_empty() { /// let level_size = deque.len(); /// let mut added = false; /// depth += 1; /// for _i in 0..level_size { /// let n = deque.pop_front(); /// added = true; /// if let Some(Some(node)) = n { /// if node.borrow().left.is_some() { deque.push_back(node.borrow().left.clone());} /// if node.borrow().right.is_some() { deque.push_back(node.borrow().right.clone());} /// } /// } /// if !added { break; } /// } /// depth /// } /// } /// ``` /// pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { match root { Some(node) => { let left = max_depth(node.borrow().left.clone()); let right = max_depth(node.borrow().right.clone()); 1 + left.max(right) }, _ => 0, } } // 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 } } }