Skip to content

Commit f8cc5d0

Browse files
author
Kohei Asai
authored
104. Maximum Depth of Binary Tree (#72)
1 parent ca90876 commit f8cc5d0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createBinaryTreeNode } from "../utilities/TreeNode";
2+
import maxDepth from "./maximumDepthOfBinaryTree";
3+
4+
describe("104. Maximum Depth of Binary Tree", () => {
5+
test("#1", () => {
6+
expect(maxDepth(createBinaryTreeNode([3, 9, 20, null, null, 15, 7]))).toBe(
7+
3
8+
);
9+
});
10+
11+
test("#2", () => {
12+
expect(maxDepth(createBinaryTreeNode([]))).toBe(0);
13+
});
14+
});

solutions/maximumDepthOfBinaryTree.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TreeNode } from "../utilities/TreeNode";
2+
3+
// 104. Maximum Depth of Binary Tree
4+
// https://leetcode.com/problems/maximum-depth-of-binary-tree/
5+
export default function maxDepth<T>(node: TreeNode<T> | null): number {
6+
return node === null
7+
? 0
8+
: Math.max(maxDepth(node.left), maxDepth(node.right)) + 1;
9+
}

0 commit comments

Comments
 (0)