Skip to content

Commit

Permalink
problem: new problem solution - 559 . Maximum Depth of N-Ary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Nov 27, 2023
1 parent e33255f commit fbdf9c9
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
104 changes: 104 additions & 0 deletions problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit fbdf9c9

Please sign in to comment.