当前位置:网站首页>设计一个打印整棵树的打印函数
设计一个打印整棵树的打印函数
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 度的二叉树:
边栏推荐
- Pytest framework
- Elegant request retry using guzzle Middleware
- Go language development code self test excellent go fuzzing usage explanation
- 模板:P6114 【模板】Lyndon 分解&Runs(字符串)
- AutoK3s v0.5.0 发布 延续简约和友好
- 聚焦工业智能化场景,华为云招募合作伙伴,帮助解决转型难题
- Esp8266/esp32 get NTP time method through timelib Library
- Disruptor local thread queue_ Workprocessor exception_ Fatalexceptionhandler--- inter thread communication work note 004
- Can Koufu open a futures account? Is it safe?
- Undefined functions or variables [explained in one article] (matlab)
猜你喜欢

Test log of unittest framework

In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened

Huawei cloud releases desktop ide codearts

集成底座方案演示说明

Yaml data driven demo

Huawei (13) - route introduction

如何编写测试用例

【观察】微软“云+端”全面创新,让混合云更简单、更灵活、更安全

HUAWEI(13)——路由引入

Esp8266 / esp32 obtenir la méthode NTP time via la Bibliothèque timelib
随机推荐
[observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
如何编写测试用例
Unittest框架
Test log of unittest framework
[live broadcast preview] at 19:00 on June 24, hcsd live broadcast -- employment guide, which will take you through the interview points for the upcoming autumn recruitment and summer internship~~
【观察】微软“云+端”全面创新,让混合云更简单、更灵活、更安全
Pytest framework
Generating test reports using the unittest framework
Yaml数据驱动演示
Which futures company is better to open an account at present? Is the service charge low and the transaction safe?
Kotlin 中list set map
Serious illness insurance covers serious illness. Which product is the best in the market? Please recommend it
Characteristics of a good product
The new download window of Google Chrome browser supports file dragging, and edge already supports
Luban developers' in-depth Forum - "insight into the new trend of the Internet of things" with Chengdu partners
How to write test cases
Why do you want to develop tea mall applet app?
Cisco(59)——Hub&Spoke MPLS
Notice on printing and distributing the Interim Measures of Beijing Municipality for the administration of housing with common property rights
Elegant request retry using guzzle Middleware