Skip to content

Commit a90c013

Browse files
committed
feat: mark done and rename max to maximum depth of binary tree
1 parent 4e75625 commit a90c013

File tree

4 files changed

+90
-90
lines changed

4 files changed

+90
-90
lines changed

src/easy/max_depth_of_binary_tree.rs

-88
This file was deleted.
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#![allow(dead_code)]
2+
use std::cell::RefCell;
3+
use std::rc::Rc;
4+
5+
type Tree = Option<Rc<RefCell<TreeNode>>>;
6+
7+
#[derive(Debug, PartialEq, Eq)]
8+
pub struct TreeNode {
9+
pub val: i32,
10+
pub left: Tree,
11+
pub right: Tree,
12+
}
13+
14+
impl TreeNode {
15+
#[inline]
16+
pub fn new(val: i32) -> Self {
17+
TreeNode {
18+
val,
19+
left: None,
20+
right: None,
21+
}
22+
}
23+
}
24+
25+
pub fn max_depth(root: Tree) -> i32 {
26+
if root.is_none() {
27+
return 0;
28+
}
29+
30+
let left_depth = max_depth(root.as_ref().unwrap().borrow().left.clone());
31+
let right_depth = max_depth(root.as_ref().unwrap().borrow().right.clone());
32+
33+
return 1 + std::cmp::max(left_depth, right_depth);
34+
}
35+
36+
/*
37+
Algorithm - Recursive DFS (Depth First Search)
38+
- If the root is None, return 0
39+
- Get the max depth of the left subtree
40+
- Get the max depth of the right subtree
41+
- Return 1 + the max of the left and right subtree
42+
43+
Complexity
44+
- Time: O(n)
45+
- Space: O(n)
46+
*/
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
52+
#[test]
53+
fn test_1() {
54+
let mut root = TreeNode::new(3);
55+
let mut left = TreeNode::new(9);
56+
let right = TreeNode::new(20);
57+
let left_left = TreeNode::new(15);
58+
let left_right = TreeNode::new(7);
59+
60+
left.left = Some(Rc::new(RefCell::new(left_left)));
61+
left.right = Some(Rc::new(RefCell::new(left_right)));
62+
root.left = Some(Rc::new(RefCell::new(left)));
63+
root.right = Some(Rc::new(RefCell::new(right)));
64+
65+
assert_eq!(max_depth(Some(Rc::new(RefCell::new(root)))), 3);
66+
}
67+
68+
#[test]
69+
fn test_2() {
70+
let mut root = TreeNode::new(1);
71+
let left = TreeNode::new(2);
72+
73+
root.left = Some(Rc::new(RefCell::new(left)));
74+
75+
assert_eq!(max_depth(Some(Rc::new(RefCell::new(root)))), 2);
76+
}
77+
78+
#[test]
79+
fn test_3() {
80+
let root = TreeNode::new(0);
81+
82+
assert_eq!(max_depth(Some(Rc::new(RefCell::new(root)))), 1);
83+
}
84+
85+
#[test]
86+
fn test_4() {
87+
assert_eq!(max_depth(None), 0);
88+
}
89+
}

src/easy/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub mod invert_binary_tree;
1212
pub mod length_of_last_word;
1313
pub mod linked_list_cycle;
1414
pub mod longest_common_prefix;
15-
pub mod max_depth_of_binary_tree;
1615
pub mod maximum_depth_of_binary_tree;
1716
pub mod merge_sorted_array;
1817
pub mod merge_two_sorted_lists;

theory/categories/4.trees/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
## Problems
6262

6363
- [x] [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | Easy | [Solution](../../../src/easy/invert_binary_tree.rs) | [Problem Description](../../../src/easy/readme.md#226-invert-binary-tree)
64-
- [ ] [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | Easy | [Solution](../../../src/easy/maximum_depth_of_binary_tree.rs) | [Problem Description](../../../src/easy/readme.md#104-maximum-depth-of-binary-tree)
64+
- [x] [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | Easy | [Solution](../../../src/easy/maximum_depth_of_binary_tree.rs) | [Problem Description](../../../src/easy/readme.md#104-maximum-depth-of-binary-tree)
6565
- [ ] [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | Easy | [Solution](../../../src/easy/diameter_of_binary_tree.rs) | [Problem Description](../../../src/easy/readme.md#543-diameter-of-binary-tree)
6666
- [ ] [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | Easy | [Solution](../../../src/easy/balanced_binary_tree.rs) | [Problem Description](../../../src/easy/readme.md#110-balanced-binary-tree)
6767
- [ ] [Same Tree](https://leetcode.com/problems/same-tree/) | Easy | [Solution](../../../src/easy/same_tree.rs) | [Problem Description](../../../src/easy/readme.md#100-same-tree)

0 commit comments

Comments
 (0)