当前位置:网站首页>Question 102: sequence traversal of binary tree
Question 102: sequence traversal of binary tree
2022-07-07 05:50:00 【Yingtai night snow】
Power button 102 topic : Sequence traversal of binary tree
Title Description
Give you the root node of the binary tree root , Returns the Sequence traversal . ( That is, layer by layer , Access all nodes from left to right ).
I/o sample

Input :root = [3,9,20,null,null,15,7]
Output :[[3],[9,20],[15,7]]
Input :root = [1]
Output :[[1]]
Input :root = []
Output :[]
solution 1, Use queues to leverage iterations
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>>res;
if(!root)
{
return res;
}
// Build queue
queue<TreeNode *>que;
que.push(root);
vector<int>tempList;
while(!que.empty())
{
int length=que.size();
res.push_back({
});
for(int i=0;i<length;i++)
{
TreeNode * temp=que.front();
que.pop();
// cout<<temp->val<<" ";
res.back().push_back(temp->val);
if(temp->left)
{
que.push(temp->left);
}
if(temp->right)
{
que.push(temp->right);
}
}
}
return res;
}
Solution 2 , Use recursion
vector<vector<int>>nums;
// Use recursive method to solve
void dns(TreeNode *root,int lever)
{
if(!root)
{
return;
}
if(nums.size()==lever)
{
nums.push_back({
});
}
nums[lever].push_back(root->val);
dns(root->left,lever+1);
dns(root->right,lever+1);
}
vector<vector<int>> levelOrder2(TreeNode* root)
{
dns(root,0);
return nums;
}
边栏推荐
- Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
- 《ClickHouse原理解析与应用实践》读书笔记(6)
- 4. 对象映射 - Mapping.Mapster
- 谈fpga和asic的区别
- 判断文件是否为DICOM文件
- C#可空类型
- Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
- 驱动开发中platform设备驱动架构详解
- 常用消息队列有哪些?
- SAP ABAP BDC (batch data communication) -018
猜你喜欢
![Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]](/img/9c/1f031400f0e201df47bd51547ff73f.png)
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]

PowerPivot——DAX(函数)

OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力

消息队列:消息积压如何处理?

JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface

Industrial Finance 3.0: financial technology of "dredging blood vessels"

Digital IC interview summary (interview experience sharing of large manufacturers)

Reptile exercises (III)

Flink SQL realizes reading and writing redis and dynamically generates hset key

不同网段之间实现GDB远程调试功能
随机推荐
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
盘点国内有哪些EDA公司?
bat 批示处理详解
Get the way to optimize the one-stop worktable of customer service
"Multimodal" concept
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
English grammar_ Noun possessive
如何提高网站权重
常用消息队列有哪些?
Go 語言的 Context 詳解
404 not found service cannot be reached in SAP WebService test
集群、分布式、微服务的区别和介绍
关于服装ERP,你知道多少?
消息队列:消息积压如何处理?
5. 数据访问 - EntityFramework集成
Mapbox Chinese map address
Message queue: how to deal with message backlog?
Hcip eighth operation
On the difference between FPGA and ASIC