diff --git a/README.md b/README.md index 9080f7e..0ba0b61 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ My approach to solving LeetCode problems typically involves the following steps: | 404 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | Algorithms | [TypeScript](./problems/algorithms/sumOfLeftLeaves/SumOfLeftLeaves.ts) | Easy | | 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | Algorithms | [TypeScript](./problems/algorithms/diameterOfBinaryTree/DiameterOfBinaryTree.ts) | Easy | | 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | Algorithms | [TypeScript](./problems/algorithms/addBinary/AddBinary.ts) | Easy | +| 559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) | Algorithms | [TypeScript](./problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts) | Easy | | ... | ... | ... | ... | ... | In this table: diff --git a/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.test.ts b/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.test.ts new file mode 100644 index 0000000..83b9437 --- /dev/null +++ b/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.test.ts @@ -0,0 +1,36 @@ +// Source : https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ +// Author : Francisco Tomas +// Date : 2023-11-27 + +import { NodeTree, maxDepth } from "./MaximumDepthOfNAryTree"; + +describe("given a n-ary tree, find its maximum depth", () => { + test("small tree", () => { + const result: number = maxDepth( + new NodeTree(1, [ + new NodeTree(3, [new NodeTree(5, []), new NodeTree(6, [])]), + new NodeTree(2, []), + new NodeTree(4, []), + ]), + ); + expect(result).toBe(3); + }); + + test("larger tree", () => { + const result: number = maxDepth( + new NodeTree(1, [ + new NodeTree(2, []), + new NodeTree(3, [ + new NodeTree(6, []), + new NodeTree(7, [new NodeTree(11, [new NodeTree(14, [])])]), + ]), + new NodeTree(4, [new NodeTree(8, [new NodeTree(12, [])])]), + new NodeTree(5, [ + new NodeTree(9, [new NodeTree(13, [])]), + new NodeTree(10, []), + ]), + ]), + ); + expect(result).toBe(5); + }); +}); diff --git a/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts b/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts new file mode 100644 index 0000000..dd528df --- /dev/null +++ b/problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts @@ -0,0 +1,104 @@ +// Source : https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ +// Author : Francisco Tomas +// Date : 2023-11-27 + +/***************************************************************************************************** + * + * Given a n-ary tree, find its maximum depth. + * + * The maximum depth is the number of nodes along the longest path from the root node down to the + * farthest leaf node. + * + * Nary-Tree input serialization is represented in their level order traversal, each group of children + * is separated by the null value (See examples). + * + * Example 1: + * + * Input: root = [1,null,3,2,4,null,5,6] + * Output: 3 + * + * Example 2: + * + * Input: root = + * [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] + * Output: 5 + * + * Constraints: + * + * The total number of nodes is in the range [0, 10^4]. + * The depth of the n-ary tree is less than or equal to 1000. + ******************************************************************************************************/ + +type Tree = NodeTree | null; + +export class NodeTree { + val: number; + children: NodeTree[]; + constructor(val?: number, children?: NodeTree[]) { + this.val = val ?? 0; + this.children = children ?? []; + } +} + +/** + * Tree -> number + * given a n-ary tree, root, find its maximum depth + * NOTES: The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. + * Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value + + * Stub: + function maxDepth(root: Tree): number {return 0} + + * Tests: + * I: root = [1,null,3,2,4,null,5,6] -> O: 3 + * I: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] -> O: 5 + + * Constraints: + * - The total number of nodes is in the range [0, 10^4]. + * - The depth of the n-ary tree is less than or equal to 1000. + */ + +/** + * Time Complexity: O(n), where n is the given tree's, root, number of nodes + * Space Complexity: O(h), where h is the given tree's, root, height (maximum depth of the recursion stack) + + * Runtime: 75ms (45.45%) + * Memory: 45.33MB (92.21%) + */ +export function maxDepthV1(root: Tree): number { + function maxDepthNode(node: NodeTree): number { + return maxDepthListOfNodes(node.children) + 1; + } + + function maxDepthListOfNodes(lon: NodeTree[]): number { + if (lon.length === 0) { + return 0; + } else { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return Math.max(maxDepthNode(lon.shift()!), maxDepthListOfNodes(lon)); + } + } + + return root !== null ? maxDepthNode(root) : 0; +} + +/** + * Time Complexity: O(n), where n is the given tree's, root, number of nodes + * Space Complexity: O(h), where h is the given tree's, root, height (maximum depth of the recursion stack) + + * Runtime: 59ms (94.81%) + * Memory: 45.82MB (77.92%) + */ +export function maxDepth(root: Tree): number { + if (root === null) { + return 0; + } else { + let depth: number = 1; + for (let i: number = 0; i < root.children.length; i++) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const curr: number = maxDepth(root.children[i]!) + 1; + depth = Math.max(depth, curr); + } + return depth; + } +}