leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历
基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行下一个循环就可以了。
C++代码:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- vector<vector<int>> levelOrder(TreeNode* root) {
- if(root==NULL) return {};
- queue<TreeNode*> q;
- TreeNode* front;
- q.push(root);
- vector<vector<int>> res;
- while(!q.empty()){
- vector<int> onelevel;
- for(int i=q.size();i>;i--){
- front=q.front();
- q.pop();
- if(front->left)
- q.push(front->left);
- if(front->right)
- q.push(front->right);
- onelevel.push_back(front->val);
- }
- res.push_back(onelevel);
- }
- return res;
- }
- };
leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历的更多相关文章
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- 102 Binary Tree Level Order Traversal 二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 ...
- [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】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
随机推荐
- curry&unCurry函数
unCurry函数与curry函数区别:curry化函数:固定部分参数,返回一个接受剩余参数的新函数,目的是为了缩小适用范围,创建一个针对性更强的函数. unCurry化函数:扩大适用范围,创建一个应 ...
- Git生成公钥.pub 及秘钥 命令
Git生成公钥.pub 及秘钥 命令 ssh-keygen -t rsa -C "******@qq.com" 将.pub公钥里面内容复制到github或者将这文件交给git管理员 ...
- 十、LaTex数学公式初步
- js node 节点 原生遍历 createNodeIterator
1.createIterator msn: https://developer.mozilla.org/en-US/docs/Web/API/Document/createNodeIterator v ...
- docker search - 搜寻镜像
使用docker search 命令可以搜索docker hub官方仓库中的镜像. # docker search --help Usage: docker search [OPTIONS] TERM ...
- 一、Linux平台部署ASP.NET、ASP.NET CORE、PHP
一.什么是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关服务器,以支持ASP.NET.ASP.NET CORE.PHP为特色,同时具备反向代理.入侵检测等重要功能.可以 ...
- CentOS7 配置阿里云yum源,非常之简单
1.进入yum的文件夹 命令:cd /etc/yum.repos.d/ 2.下载wget 命令:yum -y install wget 命令:yum install bash-completion ...
- pyqt-swf
# pyqt5界面打开flash.swf文件 from PyQt5 import QtCore, QtGui, QAxContainer, QtWidgets class Ui_Flash(QAxCo ...
- iOS的UILabel设置多行显示
label.lineBreakMode = NSLineBreakByWordWrapping; label.numberOfLines = ;
- Eigen 矩阵库学习笔记
最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...