当前位置:网站首页>【LeetCode】102.二叉树的层序遍历
【LeetCode】102.二叉树的层序遍历
2022-08-02 02:40:00 【酥酥~】
题目
给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root==nullptr)
return {
};
queue<TreeNode*> myqueue;
myqueue.emplace(root);
vector<vector<int>> result;
while(!myqueue.empty())
{
result.emplace_back(vector<int>());
int len = myqueue.size();
for(int i=0;i<len;i++)
{
TreeNode* node = myqueue.front();
result.back().emplace_back(node->val);
myqueue.pop();
if(node->left)
myqueue.emplace(node->left);
if(node->right)
myqueue.emplace(node->right);
}
}
return result;
}
};
边栏推荐
猜你喜欢

【Unity入门计划】2D Game Kit:初步了解2D游戏组成

Remember a gorm transaction and debug to solve mysql deadlock

KICAD 小封装拉线卡顿问题 解决方法

A good book for newcomers to the workplace

国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现

Ringtone 1161. Maximum In-Layer Elements and

IMU预积分的简单理解

项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error

Oracle数据类型介绍

The state status is displayed incorrectly after the openGauss switch
随机推荐
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
2022河南青训联赛第(三)场
第11章_数据库的设计规范
Outsourcing worked for three years, it was abolished...
Ringtone 1161. Maximum In-Layer Elements and
网络层解析——IP协议、地址管理、路由选择
使用self和_(下划线)的区别
MySQL索引优化实战
leetcode / anagram in string - some permutation of s1 string is a substring of s2
IPFS部署及文件上传(golang)
MySQL - CRUD operations
字符串常用方法
架构:分布式任务调度系统(SIA-Task)简介
The state status is displayed incorrectly after the openGauss switch
Analysis of the status quo of digital transformation of manufacturing enterprises
忽晴忽雨
搭建zabbix监控及邮件报警(超详细教学)
架构:微服务网关(SIA-Gateway)简介
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
微信小程序异步回调函数恶梦和解决办法