按层遍历树,要用到queue

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ret;
//ahd(root)
//a(ret)
queue<TreeNode*> que;
//a(que)
if(!root)
return ret;
que.push(root);
//dsp
while(!que.empty()){
int n=que.size();
vector<int> line;
//a(line)
while(n-->){
auto cur=que.front(); que.pop();
line.push_back(cur->val);
//a(cur)
//lk("root",cur)
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
//dsp
}
ret.push_back(line);
//dsp
}
return ret;
}
};

程序动态运行过程:http://simpledsp.com/FS/Html/lc102.html

LeetCode 102. Binary Tree Level Order Traversal 动态演示的更多相关文章

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

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

  3. leetcode 102 Binary Tree Level Order Traversal ----- java

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

  4. Java [Leetcode 102]Binary Tree Level Order Traversal

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

  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 二叉树的层次遍历 C++

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

  7. Java for 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, ...

  8. leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)

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

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

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

随机推荐

  1. hibernate validator参数校验&自定义校验注解

    参数校验:简单的就逐个手动写代码校验,推荐用Valid,使用hibernate-validator提供的,如果参数不能通过校验,报400错误,请求格式不正确: 步骤1:在参数对象的属性上添加校验注解如 ...

  2. JS跨域:jsonp、跨域资源共享、iframe+window.name

    JS跨域:jsonp.跨域资源共享.iframe+window.name :https://www.cnblogs.com/doudoublog/p/8652213.html JS中的跨域 请求跨域有 ...

  3. Java并发编程:Concurrent锁机制解析

    Java并发编程:Concurrent锁机制解析 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: # ...

  4. 2019 Multi-University Training Contest 1 - 1012 - NTT

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=6589 题解连接: https://www.cnblogs.com/xusirui/p/1122945 ...

  5. python学习第一天变量命名规范和变量作用

    变量的命名 python中的变量跟其他编程语言变量一样 1,由字母,下划线,数字组成 2,数字不能做变量名开头 3,变量名尽量有意义和短,,也可以驼峰,不要很low ,比如说是 中文,变量名很长 py ...

  6. Gradle 入门--只此一篇

    是什么? 在语法上是基于Groovy语言的(Groovy 是一种基于JVM的敏捷开发语言,可以简单的理解为强类型语言java的弱类型版本),在项目管理上是基于Ant和Maven概念的项目自动化建构工具 ...

  7. 关于Visual Studio Code 以及 一些工具

    常用插件 Path Intellisense 路径提示 open in browser 打开浏览器 ALT+B 代码块 ctrl + shift + p/或者设置(左下角右下角的齿轮)=> 命令 ...

  8. swiper轮播图设置每组显示的个数及自定义slide宽度

    一.html演示代码: <div class="swiper-container"> <div class="swiper-wrapper"& ...

  9. 基本的bash shell

    一.linux文件系统 linux文件系统结构是从Unix文件结构演进过来的.在linux文件系统中,通用的目录名用于表示一些常见的功能.如下表列出一些较为常见的Linux顶层虚拟目录名及其内容. / ...

  10. 十、.NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

    十..NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件 1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; us ...