| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using LeetCode.Core.Models; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.MaximumDepthOfNaryTree; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class MaximumDepthOfNaryTreeDepthFirstSearchRecursive : IMaximumDepthOfNaryTree |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(h), where h is the height of the tree |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="root"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int MaxDepth(Node? root) |
| 21 | 26 | | { |
| 21 | 27 | | if (root == null) |
| 1 | 28 | | { |
| 1 | 29 | | return 0; |
| | 30 | | } |
| | 31 | |
|
| 20 | 32 | | var maxChildDepth = 0; |
| | 33 | |
|
| 20 | 34 | | if (root.children != null) |
| 13 | 35 | | { |
| 13 | 36 | | maxChildDepth = root.children.Select(MaxDepth).Prepend(maxChildDepth).Max(); |
| 13 | 37 | | } |
| | 38 | |
|
| 20 | 39 | | return maxChildDepth + 1; |
| 21 | 40 | | } |
| | 41 | | } |