222. 完全二叉树的节点个数
- 题号:222
- 来源:LeetCode
- 难度:简单
- 标签:
树递归 - 语言:TypeScript
- 解法:1 个
- 作者:lmliheng
- 最近更新:2026-09-27
TypeScript · O(n) 时间 · O(n) 空间 · 更新于 2026-09-27
递归统计所有节点,未利用完全二叉树性质
typescript
/**
* @difficulty easy
* @tags 树,递归
* @time O(n)
* @space O(n)
* @note 递归统计所有节点,未利用完全二叉树性质
* @222. 完全二叉树的节点个数
*/
function countNodes(root: TreeNode | null): number {
if (root === null) return 0
return 1 + countNodes(root.left) + countNodes(root.right)
}源码:ts/leetcode/222. 完全二叉树的节点个数.ts
在 GitHub 上查看题目所在目录:lmliheng/algorithm