当前位置:网站首页>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;
}
边栏推荐
- Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
- SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
- What is message queuing?
- Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
- Unity keeps the camera behind and above the player
- Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
- 往图片添加椒盐噪声或高斯噪声
- How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
- WEB架构设计过程
- Industrial Finance 3.0: financial technology of "dredging blood vessels"
猜你喜欢
产业金融3.0:“疏通血管”的金融科技
Web authentication API compatible version information
往图片添加椒盐噪声或高斯噪声
5. Data access - entityframework integration
Differences and introduction of cluster, distributed and microservice
Leetcode: maximum number of "balloons"
4. Object mapping Mapster
上海字节面试问题及薪资福利
How does mapbox switch markup languages?
《HarmonyOS实战—入门到开发,浅析原子化服务》
随机推荐
Pytorch builds neural network to predict temperature
PowerPivot——DAX(函数)
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
Five core elements of architecture design
线性回归
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
JVM the truth you need to know
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
TCC of distributed transaction solutions
《ClickHouse原理解析与应用实践》读书笔记(6)
WEB架构设计过程
基于NCF的多模块协同实例
bat 批示处理详解
Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
Flinksql 读写pgsql
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
STM32 key state machine 2 - state simplification and long press function addition
An example of multi module collaboration based on NCF
拼多多新店如何获取免费流量,需要从哪些环节去优化,才能有效提升店内免费流量