二叉树:计算叶子节点个数

叶子节点的特征:左右孩子均为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);
	}
}
发布了235 篇原创文章 · 获赞 51 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/102885451