-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxDepth.ts
More file actions
54 lines (51 loc) · 1.31 KB
/
MaxDepth.ts
File metadata and controls
54 lines (51 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { TreeNode } from '../TreeNode'
/**
* 求树的最大深度, 使用内部递归的方式
* @param root 根节点
*/
export function maxDepthUseTraverse(root: TreeNode | null): number {
let res = 0
let depth = 0
// 内部遍历框架
function traverse(node: TreeNode | null) {
if (!node) {
// 更新最大深度
res = Math.max(depth, res)
return
}
// 入栈
depth++
traverse(node.left)
traverse(node.right)
// 出栈
depth--
}
traverse(root)
return res
}
/**
* 求树的最大深度, 使用分解问题成计算左右子树高度的方式
* @param root 根节点
*/
export function maxDepthUseDecomposition(root: TreeNode | null): number {
if (!root) {
return 0
}
// 获取左子树的最大深度
const leftMaxDepth = maxDepthUseDecomposition(root.left)
// 获取右子树的最大深度
const rightMaxDepth = maxDepthUseDecomposition(root.right)
// 比较它们并加上根节点
return Math.max(leftMaxDepth, rightMaxDepth) + 1
}
/**
* 计算最深 DOM 节点树的深度
* @param root 根节点
*/
export function maxDepthForElement(root: Element): number {
if (!root.children.length) {
return 0
}
// 比较它们并加上根节点
return Math.max(...[...root.children].map((children) => maxDepthForElement(children))) + 1
}