222. 完全二叉树的节点个数
小于 1 分钟
222. 完全二叉树的节点个数简单
解题思路:先序遍历
class Solution {
int res = 0;
public int countNodes(TreeNode root) {
if(root == null) return res;
dfs(root);
return res;
}
public void dfs(TreeNode node) {
if(node == null) return;
else {
res++;
dfs(node.left);
dfs(node.right);
}
}
}
Powered by Waline v2.15.5