当前位置:网站首页>On the sequence traversal of binary tree Ⅱ
On the sequence traversal of binary tree Ⅱ
2022-06-11 18:28:00 【HHYX.】
Sequence traversal of binary tree Ⅱ
Topic link : Sequence traversal of binary tree Ⅱ
Title Description
Give you the root node of the binary tree root , Returns its node value Bottom up sequence traversal . ( That is, from the layer where the leaf node is to the layer where the root node is , Traversal layer by layer from left to right )
Topic analysis
The topic here looks very difficult , If the binary tree is traversed from bottom to top , It seems hard to do . But you might as well change your mind , If you reverse the two-dimensional array formed by traversing the binary tree from top to bottom , Is it equivalent to bottom-up traversal . thus , The train of thought is very clear . The normal sequence traversal method of binary tree can refer to the previous blog , I won't go into that , Here, you only need to reverse the final result to pass , The code is as follows :
Code implementation
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> ret;
queue<TreeNode*> q;
if (root)
{
q.push(root);
}
int levelsize = q.size();
while (!q.empty())
{
vector<int> tmp;
while (levelsize)
{
TreeNode* cur = q.front();
q.pop();
tmp.push_back(cur->val);
if (cur->left)
{
q.push(cur->left);
}
if (cur->right)
{
q.push(cur->right);
}
levelsize--;
}
ret.push_back(tmp);
levelsize = q.size();
}
reverse(ret.begin(), ret.end());
return ret;
}

边栏推荐
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- 使用mysql判断日期是星期几
- TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生
- SA token single sign on SSO mode 2 URL redirection propagation session example
- 5 minutes to understand the red, blue and purple in the attack and defense drill
- 金融银行_催收系统简介
- Easycwmp source code analysis
- [c language] output the students within the specified score range with the structure
- HashSet collection
- MMA-Self-defining function
猜你喜欢

H.264概念
![[c language] output the students with the highest scores with a structure. There can be multiple highest scores](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[c language] output the students with the highest scores with a structure. There can be multiple highest scores

DC-DC自举电容(BOOT)几个问题
MySQL/Redis 常见面试题汇总

【新手上路常见问答】关于项目管理

神经网络与深度学习-2- 机器学习简单示例-PyTorch

炫酷的可视化工具:processing 初识
![[c language] compress strings and add markup characters](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[c language] compress strings and add markup characters

使用Visdom對損失函數進行監控
![[C语言]用结构体把平均分和低于等于平均分的学生数据输出](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
随机推荐
async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
SQL error injection 1
Feign 共享登录信息进行请求
Use transformers to convert TF model to pytorch model
Reading summary of nacos2.x source code
Various poses for text modification using sed
网络和并发编程常见面试题
SISO decoder for a general (n,n-1) SPC code(補充章節3)
[c language] output the students within the specified score range with the structure
[golang] leetcode - 292 Nim games (Mathematics)
MMA-Self-defining function
[C语言]对一个数组的元素排序后平移元素
Some thoughts on how to do a good job of operation and maintenance management
牛客刷题——把字符串转换成整数
下载代码,并编译环境的问题
Modern application of LDAP directory server
Oracle高级数据库复习
Getting started with CTF
Apipost精妙使用技巧
力扣刷题——二叉树的层序遍历