Gjort 104

This commit is contained in:
Håkon Størdal 2025-03-23 15:49:08 +01:00
parent 681be3accc
commit 114fa8da6f
2 changed files with 46 additions and 0 deletions

View file

@ -0,0 +1,6 @@
[package]
name = "maximum-depth-of-binary-tree-104"
version = "0.1.0"
edition = "2024"
[dependencies]

View 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!");
}