Gjort 104
This commit is contained in:
parent
681be3accc
commit
114fa8da6f
2 changed files with 46 additions and 0 deletions
6
maximum-depth-of-binary-tree-104/Cargo.toml
Normal file
6
maximum-depth-of-binary-tree-104/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "maximum-depth-of-binary-tree-104"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
40
maximum-depth-of-binary-tree-104/src/main.rs
Normal file
40
maximum-depth-of-binary-tree-104/src/main.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Definition for a binary tree node.
|
||||
struct Solution {}
|
||||
|
||||
#[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::rc::Rc;
|
||||
impl Solution {
|
||||
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
match root {
|
||||
None => 0,
|
||||
Some(node) => {
|
||||
1 + std::cmp::max(
|
||||
Self::max_depth(node.borrow().left.clone()),
|
||||
Self::max_depth(node.borrow().right.clone()),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue