写代码求二叉树的叶子结点数
时间: 2023-06-06 10:07:28 浏览: 83
好的,关于二叉树的叶子节点数的问题,我可以为您提供具体的代码实现。以下是一个简单的 Python 代码示例:
```
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def count_leaf_node(root: TreeNode) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
return count_leaf_node(root.left) + count_leaf_node(root.right)
```
您可以将树的根节点传递给 `count_leaf_node` 函数,并返回树的叶子节点数。
希望这可以帮助您解决您的问题。
相关问题
c语言如何求二叉树的叶子结点数
对于您的第一个问题,以下是求解二叉树叶子结点数的C语言代码示例:
```
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int countLeafNodes(struct TreeNode* root) {
if (root == NULL) {
return 0;
} else if (root->left == NULL && root->right == NULL) {
return 1;
} else {
return countLeafNodes(root->left) + countLeafNodes(root->right);
}
}
int main() {
// 创建一个二叉树
struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->right->val = 3;
root->right->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->right->left->val = 4;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->right->right->val = 5;
root->right->right->left = NULL;
root->right->right->right = NULL;
// 计算叶子结点数
int count = countLeafNodes(root);
printf("叶子结点数为:%d\n", count);
return 0;
}
```
该代码中,首先定义了一个 `TreeNode` 结构体表示二叉树节点,包含 `val` 代表节点值、`left` 代表左子结点指针、`right` 代表右子结点指针。然后定义了一个 `countLeafNodes` 函数,输入一个二叉树根节点指针,返回该二叉树叶子节点数量。函数中,如果根节点为空,返回0;如果根节点的左右子树都为空,表明是叶子节点,返回1;否则递归计算左右子树的叶子节点数量并相加。最后在 `main` 函数中创建一个二叉树,并调用 `countLeafNodes` 函数计算叶子节点数量,并输出结果。
注意,本回答仅供参考,如有疑问,请自行验证,不构成任何实际意义。
用递归求二叉树的叶子结点数C++
### 回答1:
可以这样写:
```c++
// 定义二叉树结点
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int countLeaf(TreeNode* root) {
if (root == NULL) { // 空树
return 0;
} else if (root->left == NULL && root->right == NULL) { // 叶子结点
return 1;
} else { // 非叶子结点
return countLeaf(root->left) + countLeaf(root->right);
}
}
```
递归函数 `countLeaf` 的作用是统计二叉树 `root` 中的叶子结点数。如果 `root` 为空,返回 0;如果 `root` 是叶子结点,返回 1;否则,返回左子树中叶子结点数和右子树中叶子结点数之和。
### 回答2:
递归求二叉树的叶子节点数C的方法如下:
若二叉树为空树,则叶子节点数为0。
若二叉树为单个节点,则叶子节点数为1。
若二叉树不为空且不是单个节点,则叶子节点数等于左子树的叶子节点数加上右子树的叶子节点数。
代码示例:
```python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def count_leaf_nodes(root):
if root is None: # 若根节点为空,返回0
return 0
if root.left is None and root.right is None: # 若根节点为叶子节点,返回1
return 1
return count_leaf_nodes(root.left) + count_leaf_nodes(root.right) # 递归计算左右子树的叶子节点数
# 构建一个二叉树
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
# 计算二叉树的叶子节点数
count = count_leaf_nodes(root)
print("叶子节点数:", count)
```
以上代码首先定义了一个`Node`类来表示二叉树节点,其中包含值、左子节点和右子节点。`count_leaf_nodes`函数是用来递归求解二叉树的叶子节点数。根据上述方法,首先判断根节点是否为空,若为空则返回0;然后判断根节点是否为叶子节点,若是则返回1;否则,递归调用`count_leaf_nodes`函数计算左子树和右子树的叶子节点数,然后返回二者之和。最后,将根节点传入`count_leaf_nodes`函数中并输出结果。运行结果为叶子节点数为2。
### 回答3:
递归求二叉树的叶子节点数C可以用以下方法实现:
1. 首先,判断二叉树是否为空。如果是空树,则叶子节点数C为0。
2. 如果不是空树,则进行以下操作:
- 如果当前节点的左子树和右子树都为空,即当前节点为叶子节点,则叶子节点数C为1。
- 如果当前节点的左子树为空,右子树不为空,则递归计算右子树的叶子节点数C。
- 如果当前节点的右子树为空,左子树不为空,则递归计算左子树的叶子节点数C。
- 如果当前节点的左子树和右子树都不为空,则递归计算左子树的叶子节点数C,并加上递归计算右子树的叶子节点数C,得到总的叶子节点数C。
这样,通过递归调用可以求得二叉树的叶子节点数C。
示例代码如下:
```python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def count_leaf_nodes(root):
if root is None:
return 0
if root.left is None and root.right is None:
return 1
if root.left is None:
return count_leaf_nodes(root.right)
if root.right is None:
return count_leaf_nodes(root.left)
return count_leaf_nodes(root.left) + count_leaf_nodes(root.right)
# 创建二叉树
# 1
# / \
# 2 3
# / \ / \
# 4 5 6 7
#
# 叶子节点数C为4
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)
leaf_nodes_count = count_leaf_nodes(root)
print("叶子结点数C为:", leaf_nodes_count)
```
输出结果为:
叶子节点数C为: 4