From 114fa8da6f1ad9041aebbf2380098a69d944141e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20St=C3=B8rdal?= <30749741+hakon55@users.noreply.github.com> Date: Sun, 23 Mar 2025 15:49:08 +0100 Subject: [PATCH] Gjort 104 --- maximum-depth-of-binary-tree-104/Cargo.toml | 6 +++ maximum-depth-of-binary-tree-104/src/main.rs | 40 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 maximum-depth-of-binary-tree-104/Cargo.toml create mode 100644 maximum-depth-of-binary-tree-104/src/main.rs diff --git a/maximum-depth-of-binary-tree-104/Cargo.toml b/maximum-depth-of-binary-tree-104/Cargo.toml new file mode 100644 index 0000000..a9ad825 --- /dev/null +++ b/maximum-depth-of-binary-tree-104/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "maximum-depth-of-binary-tree-104" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/maximum-depth-of-binary-tree-104/src/main.rs b/maximum-depth-of-binary-tree-104/src/main.rs new file mode 100644 index 0000000..a6d5854 --- /dev/null +++ b/maximum-depth-of-binary-tree-104/src/main.rs @@ -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>>, + pub right: Option>>, +} + +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>>) -> 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!"); +}