当前位置:网站首页>设计一个打印整棵树的打印函数
设计一个打印整棵树的打印函数
2022-06-21 16:01:00 【明朗晨光】
/************************************************************************* > File Name: 030.打印整棵树.cpp > Author: Maureen > Mail: [email protected] > Created Time: 二 6/21 12:16:54 2022 ************************************************************************/
#include <iostream>
using namespace std;
class TreeNode {
public:
int value;
TreeNode *left;
TreeNode *right;
TreeNode(int v) : value(v) {
}
};
string getSpace(int num) {
string space = " ";
string str = "";
for (int i = 0; i < num; i++) {
str += space;
}
return str;
}
void printInOrder(TreeNode *root, int height, string to, int len) {
if (root == nullptr) return ;
printInOrder(root->right, height + 1, "v", len);
string val = to + to_string(root->value) + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
cout << getSpace(height * len) + val << endl;
printInOrder(root->left, height + 1, "^", len);
}
void printTree(TreeNode *root) {
cout << "Binary Tree:" << endl;
printInOrder(root, 0, "H", 17);
cout << endl;
}
int main() {
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(-222222222);
root->right = new TreeNode(3);
root->left->left = new TreeNode(INT_MIN);
root->right->left = new TreeNode(55555555);
root->right->right = new TreeNode(66);
root->left->left->right = new TreeNode(777);
printTree(root);
root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->right->left = new TreeNode(5);
root->right->right = new TreeNode(6);
root->left->left->right = new TreeNode(7);
printTree(root);
root = new TreeNode(1);
root->left = new TreeNode(1);
root->right = new TreeNode(1);
root->left->left = new TreeNode(1);
root->right->left = new TreeNode(1);
root->right->right = new TreeNode(1);
root->left->left->right = new TreeNode(1);
printTree(root); //H1H表示头,^1^表示连接到左边上方离我最近的,v1v表示连接到左边下方离我最近的
return 0;
}
得到的结果就是一棵逆时针旋转了 90 度的二叉树:
边栏推荐
- In 2021 database market, aerospike competes with top manufacturers
- qtcreator报错解决
- 我给航母做3D还原:这三处细节,太震撼了…
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- Pytest framework implements pre post processing
- Yaml数据驱动演示
- Online JSON to yaml tool
- IDC咨询:2022年中国关系型数据库软件市场,变革即将到来
- Google Play Academy 组队 PK 赛,正式开赛!
- 为什么要做茶叶商城小程序app开发?
猜你喜欢
随机推荐
7 tips for writing effective help documents
关于产品运营策略
Overseas new things | software developer "dynaboard" seed round raised US $6.6 million to develop low code platform to connect design, products and developers
强化学习入门项目spinning up(1)安装
模板:P6114 【模板】Lyndon 分解&Runs(字符串)
Kotlin 中list set map
Pytest--生成测试报告
The first atlas showing the development history of the database in China was officially released!
Why do you want to develop tea mall applet app?
云原生监控系统·夜莺近期新功能一览,解决多个生产痛点
【1108. IP 地址无效化】
d改进翻译
聚焦工业智能化场景,华为云招募合作伙伴,帮助解决转型难题
Characteristics of a good product
实战---商场登录测试
Huawei cloud releases desktop ide codearts
Circular of the State Council on regulating the management of the rental of housing with common property rights (for Trial Implementation)
I dare to break in, I can create! Gaussdb proposition of the 8th "Internet +" competition is open for registration!
Machine learning model monitoring (Apria)
Unittest框架








