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, level by level).
For example:Given binary tree [3,9,20,null,null,15,7]
/ \
/ \
return its level order traversal as:
[
[],
[,],
[,]
]
二叉树的层次遍历使用队列来实现,很大程度上类似于求树的最大深度。
第一种解法:开始时当前结点数curLevelCount=1,nextLevelCount=0,根据curLevelCount来输入每一层的结点值,当其左右子树存在时,将左右子树存入队列,并nextLevelCount++,每弹出一个当前层的结点时,curLevelCount--,当其为0时,再创建一个新的vector。
C++:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res={};
queue<TreeNode*> q;
if(!root)
return res;
q.push(root);
int curLevelCount=,nextLevelCount=;
vector<int> temp={};
while(!q.empty()){
TreeNode* cur=q.front();
temp.push_back(cur->val);
q.pop();
curLevelCount--;
if(cur->left){
q.push(cur->left);
nextLevelCount++;
}
if(cur->right){
q.push(cur->right);
nextLevelCount++;
}
if(curLevelCount==){
res.push_back(temp);
temp={};
curLevelCount=nextLevelCount;
nextLevelCount=;
}
}
return res;
}
第二种方法不用参数计算每一层结点个数,利用for循环,每一层时利用队列的大小来计算每一层的结点数。两层循环嵌套,效率比较低。
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res={};
queue<TreeNode*> q;
if(!root)
return res;
q.push(root);
while(!q.empty()){
vector<int> temp={};
for(int i=q.size();i>;i--){
TreeNode* cur=q.front();
temp.push_back(cur->val);
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
res.push_back(temp);
}
return res;
}
LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++的更多相关文章
- leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历
基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...
- 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 返回其 ...
随机推荐
- js+ajax编码三级联动
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- python excle写数据
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/4/24 10:30 # @File : Excle写.py # @Softw ...
- 百度ip定位城市接口调用
http://lbsyun.baidu.com/index.php?title=webapi/ip-api require 'rubygems' require 'json' print ARGV p ...
- 西门子S7-200SMART PLC视频教程(百度网盘)
西门子S7-200SMART PLC视频教程(百度网盘)西门子S7-200 SMART PLC是西门子公司推出的高性价比小型plc,是国内广泛使用的S7-200PLC的更新换代产品. 以下是关于S7- ...
- Day 16 模块和包的导入
包的认识 包通过文件夹来管理一些列功能相近的模块 包:一系列模块的集合体 重点:包中一定有一个专门来管理包中所有模块的文件 包名:存放一系列模块的文件夹名字 包名(包对象)存放的是管理模块的那个文件地 ...
- Application、QueryString、session、cookie、ViewState、Server.Transfer等
Application: WebForm1.aspx: protected void Button1_Click(object sender, EventArgs e) { ; Response.Re ...
- 1080P60视频源---verilog
1080P60视频源---verilog `timescale 1ns / 1ps ////////////////////////////////////////////////////////// ...
- HashMap与LinkedHashMap的区别
/** * remark: * HashMap与LinkedHashMap的区别 * 这里必须使用LinkedHashMap: * 原因 ...
- Azure CosmosDB (9) Unique Key Constraints
<Windows Azure Platform 系列文章目录> 在Azure Cosmos DB中,还支持Unique Key Constraints(唯一键约束). 我们可以在Azure ...
- 恢复Windows 10自带的微软正黑字体
突然发现 在word中 Microsoft JhengHei 字体没有了,一查在C:\windows\fonts\msjh.ttc文件还在. Windows Registry Editor Vers ...