剑指Offer21 二叉树的层序遍历

 /*************************************************************************
> File Name: 21_PrintTreeTopToBottom.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月30日 星期二 20时24分53秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <bits/stdc++.h> using namespace std; // 二叉树结构体
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
}; // 层序遍历
void PrintTreeTopToBottom(TreeNode* root)
{
if (root == NULL)
return; vector<TreeNode*> vec;
vec.push_back(root); int cur = ;
while (cur < vec.size())
{
printf("%d ", vec[cur]->val);
if (vec[cur]->left)
vec.push_back(vec[cur]->left);
if (vec[cur]->right)
vec.push_back(vec[cur]->right);
++cur;
}
printf("\n");
} TreeNode* createTree()
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = ;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = ;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = ;
root->right->left = NULL;
root->right->right = NULL;
root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->val = ;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->val = ;
root->left->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->left->val = ;
root->left->right->left->left = NULL;
root->left->right->left->right = NULL;
root->left->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->right->val = ;
root->left->right->right->left = NULL;
root->left->right->right->right = NULL; return root;
} int main()
{
TreeNode* test = createTree();
PrintTreeTopToBottom(test);
}
上一篇:(1)构造方法和方法重载 (2)this关键字 (3)方法的传参和递归调用


下一篇:lpad函数