20240603 Finished

This commit is contained in:
jackfiled 2024-06-03 11:27:57 +08:00
parent c3d977118b
commit e1eb4ffa16
2 changed files with 72 additions and 1 deletions

View File

@ -138,4 +138,5 @@ mod p71_simplify_path;
mod p155_min_stack;
mod p150_evaluate_reverse_polish_notation;
mod p224_basic_calculator;
mod p21_merge_two_sorted_lists;
mod p21_merge_two_sorted_lists;
mod p104_maximum_depth_of_binary_tree;

View File

@ -0,0 +1,70 @@
/**
* [104] Maximum Depth of Binary Tree
*/
pub struct Solution {}
use crate::util::tree::{to_tree, TreeNode};
// submission codes start here
// 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::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
impl Solution {
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
let mut queue = VecDeque::new();
let mut depth = 0;
if let Some(node) = root {
queue.push_back(node);
}
while !queue.is_empty() {
let level = queue.len();
depth += 1;
for i in 0..level {
let node = queue.pop_front().unwrap();
if let Some(left) = &node.borrow().left {
queue.push_back(Rc::clone(left));
};
if let Some(right) = &node.borrow().right {
queue.push_back(Rc::clone(right));
};
}
}
depth
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_104() {}
}