打印一颗满二叉树
效果如图:
先看main()函数
int main() { node* T;//定义一个T作为树根节点 CreateTree(T);//生成一棵树 cout info = str; createTree(t->left); createTree(t->right); } }
-
求树的深度
int depth(node* root) { if (root == nullptr) { return 0; } int left = depth(root->left); int right = depth(root->right); return max(left, right) + 1; }
-
打印树
感觉太繁琐了,直接拿去用就行,不需要知道细节。
//打印一棵满二叉树,只能打印满二叉树,节点数目最好不要超过10 void print(node*& root) { //存放打印的二叉树 char str[10][100] = {}; queue q; int h = depth(root); q.push(root); int index = 0; while (!q.empty()) { int size = q.size(); //存放每一层的节点 vector list; for (int i = 0; i info); //cout info; if (temp->left != nullptr) { q.push(temp->left); } if (temp->right != nullptr) { q.push(temp->right); } } bool flag = true; int j = 0; //打印前面部分空白 while (j left); createTree(t->right); } } //树的深度 int depth(node* root) { if (root == nullptr) { return 0; } int left = depth(root->left); int right = depth(root->right); return max(left, right) + 1; } //打印一棵树满二叉树,只能打印满二叉树,节点数目最好不要超过10 void print(node*& root) { //存放打印的二叉树 char str[10][100] = {}; queue q; int h = depth(root); q.push(root); int index = 0; while (!q.empty()) { int size = q.size(); //存放每一层的节点 vector list; for (int i = 0; i info); //cout info; if (temp->left != nullptr) { q.push(temp->left); } if (temp->right != nullptr) { q.push(temp->right); } } bool flag = true; int j = 0; //打印前面部分空白 while (j