叶子节点的特征:左右孩子均为NULL
代码语言:javascript
复制
struct node { int val; node *left, *right; };
int countLeaf(node *root) {
if (!root) return 0;
else {
if (!root->left && !root->right) return 1;
return countLeaf(root->left) + countLeaf(root->right);
}
}
叶子节点的特征:左右孩子均为NULL
struct node { int val; node *left, *right; };
int countLeaf(node *root) {
if (!root) return 0;
else {
if (!root->left && !root->right) return 1;
return countLeaf(root->left) + countLeaf(root->right);
}
}