当前位置:网站首页>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;
}
边栏推荐
- Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
- Dynamic memory management
- Flinksql read / write PgSQL
- I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
- OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
- Polynomial locus of order 5
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- win配置pm2开机自启node项目
- Go 語言的 Context 詳解
- MySQL-CentOS7通过YUM安装MySQL
猜你喜欢
SQL query: subtract the previous row from the next row and make corresponding calculations
ForkJoin最全详解(从原理设计到使用图解)
How does mapbox switch markup languages?
Differences and introduction of cluster, distributed and microservice
Digital IC interview summary (interview experience sharing of large manufacturers)
Différenciation et introduction des services groupés, distribués et microservices
Modes of optical fiber - single mode and multimode
随机生成session_id
常用消息队列有哪些?
消息队列:消息积压如何处理?
随机推荐
Digital IC interview summary (interview experience sharing of large manufacturers)
5. 数据访问 - EntityFramework集成
Get the way to optimize the one-stop worktable of customer service
404 not found service cannot be reached in SAP WebService test
Polynomial locus of order 5
The 2022 China low / no code Market Research and model selection evaluation report was released
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
make makefile cmake qmake都是什么,有什么区别?
CVE-2021-3156 漏洞复现笔记
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
SQL query: subtract the previous row from the next row and make corresponding calculations
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
Sidecar mode
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
集群、分布式、微服务的区别和介绍
English grammar_ Noun possessive
Getting started with DES encryption
Paper reading [semantic tag enlarged xlnv model for video captioning]