求一个数组构成的二叉树的叶子节点的个数c++完整代码
时间: 2024-01-27 07:03:37 浏览: 33
好的,以下是求一个数组构成的二叉树的叶子节点个数的 C++ 完整代码:
```c++
#include <iostream>
#include <vector>
using namespace std;
int count_leaf_nodes(vector<int>& arr) {
int count = 0;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] == -1) {
count++;
}
}
return count;
}
int main() {
vector<int> arr{1, 2, 3, -1, 4, -1, 5};
int count = count_leaf_nodes(arr);
cout << "叶子节点个数为:" << count << endl;
return 0;
}
```
这个程序定义了一个 `count_leaf_nodes` 函数,它的参数是一个 vector<int> 类型的数组,返回值是叶子节点的个数。函数的实现和之前我给出的 Python 代码一样。主函数中定义了一个数组 `arr`,将它作为参数传递给 `count_leaf_nodes` 函数,并将返回值输出。
相关问题
C++非递归求二叉树叶子节点个数
可以使用迭代法(非递归)来遍历二叉树,然后统计叶子节点的个数。下面是一个示例代码:
```C++
#include <iostream>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int countLeaves(TreeNode* root) {
if (!root) return 0;
stack<TreeNode*> s;
s.push(root);
int count = 0;
while (!s.empty()) {
TreeNode* node = s.top();
s.pop();
if (!node->left && !node->right) count++; // 如果是叶子节点,计数器加1
if (node->right) s.push(node->right); // 先将右子节点入栈
if (node->left) s.push(node->left); // 再将左子节点入栈
}
return count;
}
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
cout << countLeaves(root) << endl; // 输出叶子节点个数
return 0;
}
```
该算法的时间复杂度为O(n),空间复杂度为O(h),其中n是二叉树的节点个数,h是二叉树的高度。
创建二叉树。 求二叉树的叶子节点代码
创建一个二叉树的一般步骤如下:
1. 定义二叉树的节点结构体,包括节点的值,左子树和右子树指针。
2. 从根节点开始,按照先序遍历的顺序输入二叉树的节点值,如果节点的值为 null,则将该节点设置为 null。
3. 递归地创建左子树和右子树。
下面是一个简单的 C++ 代码示例:
```
#include<iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* buildTree() {
int val;
cin >> val;
if (val == -1) {
return NULL;
}
TreeNode* root = new TreeNode(val);
root->left = buildTree();
root->right = buildTree();
return root;
}
```
至于求二叉树的叶子节点,可以使用递归的方法进行求解。二叉树的叶子节点是指没有左右子树的节点。因此,在递归过程中,如果当前节点为空,则返回 0,否则如果当前节点的左右子树都为空,则说明当前节点是叶子节点,返回 1,否则递归计算左子树和右子树中叶子节点的个数之和。
下面是一个简单的 C++ 代码示例:
```
int countLeaf(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right == NULL) {
return 1;
}
return countLeaf(root->left) + countLeaf(root->right);
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)