剑指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);
}
剑指Offer21 二叉树的层序遍历的更多相关文章
- 剑指offer 二叉树的层序遍历
剑指offer 牛客网 二叉树的层序遍历 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 09:33:16 2019 @ ...
- 剑指Offer——二叉树
剑指Offer--二叉树 前言 数据结构通常是编程面试中考察的重点.在参加面试之前,应聘者需要熟练掌握链表.树.栈.队列和哈希表等数据结构,以及它们的操作.本片博文主要讲解二叉树操作的相关知识,主要包 ...
- 二叉树的层序遍历 BFS
二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode之二叉树的层序遍历
1.题目描述 2.题目分析 二叉树的层序遍历主要算法思想是使用 队列这一数据结构实现,这个数据结构多应用在和 图相关的算法.例如图的广度优先遍历就可以使用队列的方法实现.本题的关键在于如何识别出一层已 ...
- LeetCode 102. 二叉树的层序遍历 | Python
102. 二叉树的层序遍历 题目来源:https://leetcode-cn.com/problems/binary-tree-level-order-traversal 题目 给你一个二叉树,请你返 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- 五三想休息,今天还学习,图解二叉树的层序遍历BFS(广度优先)模板,附面试题题解
壹 ❀ 引 我在从JS执行栈角度图解递归以及二叉树的前.中.后遍历的底层差异一文中,从一个最基本的数组遍历引出递归,在掌握递归的书写规则后,又从JS执行栈角度解释了二叉树三种深度优先(前序.中序后序) ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
随机推荐
- OC:属性的内部实现原理、dealloc内释放实例变量、便利构造器方法的实现原理、collection的内存管理
代码: // // main.m #import <Foundation/Foundation.h> #import "Person.h" #import " ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]
三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...
- visual studio 设计器上出现蓝色的点和箭头
visual studio 设计器上出现蓝色的点和箭头: 突然发现打开visual studio 的时候出现了很多蓝色的点和箭头,解决办法是:按组合快捷键 Ctrl+R,Ctrl+W 或 Ctrl+E ...
- chart.js接口开发:X轴步长和Labels旋转角
一. 当初为什么选择chart.js 当初项目使用库是Zepto,Zepto能支持的chart处理库太少.也是为了使得项目比较轻量化,所以选择了chart.js. 但是最后的显示结果实在太差,放弃了c ...
- 获取oracle 表字段,表名,以及主键之类等等的信息
数据库版本号:select * from v$version 数据库名:select * from v$instance 注意: 我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错 ...
- ARM architectures
https://gitorious.org/freebsd/freebsd/raw/56c5165837bf08f50ca4a08c6b2da91f73852960:sys/arm/include/a ...
- The Aggregate Magic Algorithms
http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...
- 使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域
UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式. //首先进行初始化 locationCollation = [UILocalizedIndexedColla ...
- 【S16】了解如何把vector和string数据传给旧的API
1.尽量使用vector和string替换数组,但是老的代码还是使用数组.如果老的接口期望是数组,怎么办? 需要把vector和string,暴露出数组接口,也就是第一个元素的地址. 2.考虑方法Do ...
- IOS 7 Study - UIDatePicker
Picking the Date and Time with UIDatePicker effect: 1. declaring a property of type UIDatePicker #im ...