|
| 1 | +/// @number 104 |
| 2 | +/// @title Maximum Depth of Binary Tree |
| 3 | +/// @url https://leetcode.com/problems/maximum-depth-of-binary-tree |
| 4 | +/// @difficulty easy |
| 5 | +
|
| 6 | +//Given a binary tree, find its maximum depth. |
| 7 | +// |
| 8 | +// The maximum depth is the number of nodes along the longest path from the root |
| 9 | +// node down to the farthest leaf node. |
| 10 | +// |
| 11 | +// Note: A leaf is a node with no children. |
| 12 | +// |
| 13 | +// Example: |
| 14 | +// |
| 15 | +// Given binary tree [3,9,20,null,null,15,7], |
| 16 | +// |
| 17 | +// |
| 18 | +// 3 |
| 19 | +// / \ |
| 20 | +// 9 20 |
| 21 | +// / \ |
| 22 | +// 15 7 |
| 23 | +// |
| 24 | +// return its depth = 3. |
| 25 | +// Related Topics Tree Depth-first Search |
| 26 | +// 👍 2511 👎 73 |
| 27 | + |
| 28 | + |
| 29 | +use std::rc::Rc; |
| 30 | +use std::cell::RefCell; |
| 31 | +use std::cmp::max; |
| 32 | + |
| 33 | +struct Solution; |
| 34 | +#[derive(Debug, PartialEq, Eq)] |
| 35 | +pub struct TreeNode { |
| 36 | + pub val: i32, |
| 37 | + pub left: Option<Rc<RefCell<TreeNode>>>, |
| 38 | + pub right: Option<Rc<RefCell<TreeNode>>>, |
| 39 | +} |
| 40 | + |
| 41 | +impl TreeNode { |
| 42 | + #[inline] |
| 43 | + pub fn new(val: i32) -> Self { |
| 44 | + TreeNode { |
| 45 | + val, |
| 46 | + left: None, |
| 47 | + right: None |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +//leetcode submit region begin(Prohibit modification and deletion) |
| 52 | +// Definition for a binary tree node. |
| 53 | + |
| 54 | +impl Solution { |
| 55 | + pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 56 | + // using reference to avoid clone |
| 57 | + pub fn _max_depth(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 { |
| 58 | + if let Some(root_node) = root { |
| 59 | + let node_borrow = root_node.borrow(); |
| 60 | + let left_depth = _max_depth(&node_borrow.left); |
| 61 | + let right_depth = _max_depth(&node_borrow.right); |
| 62 | + std::cmp::max(left_depth, right_depth) + 1 |
| 63 | + }else { |
| 64 | + 0 |
| 65 | + } |
| 66 | + } |
| 67 | + _max_depth(&root) |
| 68 | + } |
| 69 | +} |
| 70 | +//leetcode submit region end(Prohibit modification and deletion) |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod test { |
| 77 | + use crate::maximum_depth_of_binary_tree::{Solution, TreeNode}; |
| 78 | + use std::rc::Rc; |
| 79 | + use std::cell::RefCell; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn test() { |
| 83 | + let node15 = TreeNode::new(15); |
| 84 | + let node7 = TreeNode::new(7); |
| 85 | + |
| 86 | + let mut node20 = TreeNode::new(20); |
| 87 | + node20.left = Some(Rc::new(RefCell::new(node15))); |
| 88 | + node20.right = Some(Rc::new(RefCell::new(node7))); |
| 89 | + |
| 90 | + let node9 = TreeNode::new(9); |
| 91 | + |
| 92 | + let mut node3 = TreeNode::new(3); |
| 93 | + node3.left = Some(Rc::new(RefCell::new(node20))); |
| 94 | + node3.right = Some(Rc::new(RefCell::new(node9))); |
| 95 | + |
| 96 | + assert_eq!(3, Solution::max_depth(Some(Rc::new(RefCell::new(node3))))); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test2() { |
| 101 | + assert_eq!(0, Solution::max_depth(None)) |
| 102 | + } |
| 103 | +} |
0 commit comments