基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行下一个循环就可以了。

C++代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<vector<int>> levelOrder(TreeNode* root) {
  13. if(root==NULL) return {};
  14. queue<TreeNode*> q;
  15. TreeNode* front;
  16. q.push(root);
  17. vector<vector<int>> res;
  18.  
  19. while(!q.empty()){
  20. vector<int> onelevel;
  21. for(int i=q.size();i>;i--){
  22. front=q.front();
  23. q.pop();
  24. if(front->left)
  25. q.push(front->left);
  26. if(front->right)
  27. q.push(front->right);
  28. onelevel.push_back(front->val);
  29. }
  30. res.push_back(onelevel);
  31. }
  32. return res;
  33. }
  34. };

leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历的更多相关文章

  1. 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, ...

  2. 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 ...

  3. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

  4. 102 Binary Tree Level Order Traversal 二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    ...

  5. [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, ...

  6. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  7. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  8. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  9. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

随机推荐

  1. curry&unCurry函数

    unCurry函数与curry函数区别:curry化函数:固定部分参数,返回一个接受剩余参数的新函数,目的是为了缩小适用范围,创建一个针对性更强的函数. unCurry化函数:扩大适用范围,创建一个应 ...

  2. Git生成公钥.pub 及秘钥 命令

    Git生成公钥.pub 及秘钥 命令 ssh-keygen -t rsa -C "******@qq.com" 将.pub公钥里面内容复制到github或者将这文件交给git管理员 ...

  3. 十、LaTex数学公式初步

  4. js node 节点 原生遍历 createNodeIterator

    1.createIterator msn: https://developer.mozilla.org/en-US/docs/Web/API/Document/createNodeIterator v ...

  5. docker search - 搜寻镜像

    使用docker search 命令可以搜索docker hub官方仓库中的镜像. # docker search --help Usage: docker search [OPTIONS] TERM ...

  6. 一、Linux平台部署ASP.NET、ASP.NET CORE、PHP

    一.什么是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关服务器,以支持ASP.NET.ASP.NET CORE.PHP为特色,同时具备反向代理.入侵检测等重要功能.可以 ...

  7. CentOS7 配置阿里云yum源,非常之简单

    1.进入yum的文件夹 命令:cd   /etc/yum.repos.d/ 2.下载wget 命令:yum -y install wget 命令:yum install bash-completion ...

  8. pyqt-swf

    # pyqt5界面打开flash.swf文件 from PyQt5 import QtCore, QtGui, QAxContainer, QtWidgets class Ui_Flash(QAxCo ...

  9. iOS的UILabel设置多行显示

    label.lineBreakMode = NSLineBreakByWordWrapping; label.numberOfLines = ;

  10. Eigen 矩阵库学习笔记

    最近为了在C++中使用矩阵运算,简单学习了一下Eigen矩阵库.Eigen比Armadillo相对底层一点,但是只需要添加头文库即可使用,不使用额外的编译和安装过程. 基本定义 Matrix3f是3* ...