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 返回其 ...
随机推荐
- 一篇关于for循环的简单题练习,
package practice; public class Practice { public static void main(String[] args) { 7. * ** ...
- ES的索引、type、document、filer、mapping、id
一.ES的存储结构 1.索引 es 中存储数据的基本单位,比如说你现在要在 es 中存储一些订单数据,你就应该在 es 中创建一个索引 order_idx,所有的订单数据就都写到这个索引里面去.看了一 ...
- XXE漏洞攻击与防御
转自https://www.jianshu.com/p/7325b2ef8fc9 0x01 XML基础 在聊XXE之前,先说说相关的XML知识吧. 定义 XML用于标记电子文件使其具有结构性的标记语言 ...
- 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写
1. 在原服务端项目 ws_server中添加两个bean 1.1 添加两个类 User 和 Clazz package com.lonely.pojo; public class User { ...
- 缺少库libresolv
"_dns_free_resource_record", referenced from: -[xxxx processRecord:length:] in xxx.a(xxx.o ...
- 初试 pyhton 简易采集
一.安装软件(用eclispe 搭建好环境好,没有取省自动补全编写代码会很卡,最后选用sumblie) eclispe 用的windows 32 4.31 python 用的 4.3.3 下载地 ...
- 脚本_通过进程与端口判断myslq服务
#!bin/bashif [[ $port -eq 1 || $porcess -eq 2 ]];then #通过条件判断端口和进程执行的返回值. echo "mysql is s ...
- 010-监控windows主机
1)下载windows的zabbix_agent下载地址:https://www.zabbix.com/download 下载客户端并解压到指定目录D:\zabbix,解压后有两个目录:bin和con ...
- SSH整合——登录模块
1.导包——参照我的GitHub Hibernate hibernate/lib/required hibernate/lib/jpa 数据库驱动 Struts2 struts-blank.war/W ...
- 22_4mybatis——动态SQL
1.创建maven工程并导入坐标 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE confi ...