cpp打印出“树”的值
时间: 2023-08-17 21:05:11 浏览: 40
如果树是以链表的形式存储,可以通过遍历整个链表,打印每个节点的值来打印出树的值。
如果树是以数组的形式存储,可以通过递归遍历每个节点,打印每个节点的值来打印出树的值。例如:
```cpp
// Node结构体定义
struct Node {
int val;
vector<Node*> children;
};
// 递归遍历每个节点,打印节点值
void printTree(Node* root) {
if (!root) {
return;
}
cout << root->val << " ";
for (auto child : root->children) {
printTree(child);
}
}
```
其中,Node结构体表示树中的一个节点,val表示该节点的值,children表示该节点的子节点。函数printTree遍历树的每个节点,打印每个节点的值。
相关问题
圣诞树网页代码c➕➕
以下是一个用C++编写的圣诞树网页代码的示例:
```cpp
#include <iostream>
using namespace std;
int main() {
int height = 10; // 圣诞树的高度
// 打印圣诞树的上半部分
for (int i = 0; i < height; i++) {
for (int j = 0; j < height - i - 1; j++) {
cout << " ";
}
for (int k = 0; k < 2 * i + 1; k++) {
cout << "*";
}
cout << endl;
}
// 打印圣诞树的树干
for (int i = 0; i < height - 1; i++) {
cout << " ";
}
cout << "*" << endl;
return 0;
}
```
这段代码会打印出一个高度为10的圣诞树,树干由一个星号组成,树的上半部分由逐渐增多的星号组成。你可以根据需要调整`height`变量的值来改变圣诞树的高度。
二叉树的树状打印输出c++
以下是C++中二叉树的树状打印输出的代码实现:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 二叉树节点结构体
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 获取二叉树的深度
int getDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
return max(leftDepth, rightDepth) + 1;
}
// 中序遍历二叉树,将节点竖向的位置保存到vector容器中
void inorder(TreeNode* root, vector<string>& res, int depth, int pos) {
if (root == NULL) {
return;
}
// 遍历左子树
inorder(root->left, res, depth + 1, pos * 2);
// 处理当前节点
string str(depth * 2, ' '); // 根据节点所在层数确定节点前面的空格数
str += to_string(root->val); // 将节点值转换为字符串
res[pos] = str; // 将节点字符串保存到vector容器中
// 遍历右子树
inorder(root->right, res, depth + 1, pos * 2 + 1);
}
// 输出二叉树
void printTree(TreeNode* root) {
if (root == NULL) {
return;
}
int depth = getDepth(root); // 获取二叉树的深度
int width = (1 << depth) - 1; // 根据二叉树的深度确定字符串长度
vector<string> res(width, string(depth * 2, ' ')); // 初始化vector容器
inorder(root, res, 0, 0); // 中序遍历二叉树,将节点竖向的位置保存到vector容器中
for (int i = 0; i < width; i++) {
cout << res[i] << endl; // 输出二叉树
}
}
int main() {
// 创建二叉树
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->right = new TreeNode(4);
root->right->left = new TreeNode(5);
// 输出二叉树
printTree(root);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)