0%

LeetCode-222

题目

Snipaste_2020-11-24_10-21-33.png

结果

Snipaste_2020-11-24_10-21-41.png

代码

普世众生的解法

1
2
3
4
5
6
7
8
class Solution {
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + countNodes(root.left) + countNodes(root.right);
}
}

复杂度

时间复杂度:O(n),n为节点个数

空间复杂度:O(log n)