当前位置:网站首页>Design a print function to print the whole tree
Design a print function to print the whole tree
2022-06-21 17:19:00 【Bright morning light】
/************************************************************************* > File Name: 030. Print the whole tree .cpp > Author: Maureen > Mail: [email protected] > Created Time: Two 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 Said the head ,^1^ It means to connect to the nearest one on the top left ,v1v Indicates that it is connected to the one closest to me at the bottom left
return 0;
}
The result is an anticlockwise rotation 90 Degree binary tree :
边栏推荐
- Yaml file details
- 之前的装机记录
- Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
- The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information
- 建立自己的网站(11)
- [1108. IP address invalidation]
- 垃圾回收器
- Circular of the State Council on regulating the management of the rental of housing with common property rights (for Trial Implementation)
- IDC Consulting: in 2022, China's relational database software market is about to change
- Can Koufu open a futures account? Is it safe?
猜你喜欢

IDC Consulting: in 2022, China's relational database software market is about to change

The first atlas showing the development history of the database in China was officially released!

The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information

很多軟件公司,其實都是“笑話”

Huawei (13) - route introduction

Yaml数据驱动演示
![[ROS2 基础] Navigation2 导航系统介绍](/img/7f/ad6af972460d7a78fb28d9b4c24477.png)
[ROS2 基础] Navigation2 导航系统介绍

In 2021 database market, aerospike competes with top manufacturers

Online text list batch add line number tool

Necessary for data analysis: 6 steps +5 types +2 analysis methods
随机推荐
Necessary for data analysis: 6 steps +5 types +2 analysis methods
UDP和TCP的对比
Oracle JDBC 驱动
gp中的decode函数实现
In 2021 database market, aerospike competes with top manufacturers
容器云是什么意思?与堡垒机有什么区别?
Online JSON to yaml tool
How to open an account for futures agricultural products? How much is the handling charge?
Do Internet companies do unit tests? Is it necessary to do unit testing for the needs of the bank?
Move Protocol Beta测试版稳定,临时决定奖池规模再扩大
智能制造的下一站:云原生+边缘计算双轮驱动
第13周总结博客(校历第15周)动态规划总结
Fisher信息量检测对抗样本代码详解
Unittest框架的测试日志
Why do you want to develop tea mall applet app?
[mathematical modeling] Matlab Application Practice Series (95) - application cases of time series prediction (with matlab code)
二叉树的序列化与反序列化
强化学习入门项目spinning up(1)安装
叩富网可以开期货账户吗?安全吗?
Unittest框架