leetcode 二叉树的层次遍历 II(Binary Tree Level Order Traversal II)
目录
- 题目描述:
- 示例:
- 解法:
题目描述:
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例:
给定二叉树 [3,9,20,null,null,15,7]
,
3/ \9 20/ \15 7
返回其自底向上的层次遍历为:
[[15,7],[9,20],[3]]
解法:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:vector<vector<int>> levelOrderBottom(TreeNode* root) {vector<vector<int>> res;if(root){vector<int> level;vector<TreeNode*> cur;cur.push_back(root);vector<TreeNode*> nxt;while(!cur.empty()){nxt.clear();level.clear();for(TreeNode* node : cur){level.push_back(node->val);if(node->left){nxt.push_back(node->left);}if(node->right){nxt.push_back(node->right);}}res.push_back(level);cur = nxt;}}res = vector<vector<int>>(res.rbegin(), res.rend());return res;}
};
转载于:https://www.cnblogs.com/zhanzq/p/10557105.html
leetcode 二叉树的层次遍历 II(Binary Tree Level Order Traversal II)相关推荐
- LeetCode 107. Binary Tree Level Order Traversal II
LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...
- LeetCode: 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- Binary Tree Level Order Traversal II 解题思路
思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...
- 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- Leetcode | 107. Binary Tree Level Order Traversal II
题目:二叉树的层次遍历 II 1. 代码①:深度优先搜索(链接) /*** Definition for a binary tree node.* struct TreeNode {* int val ...
- [Leetcode]@python 107. Binary Tree Level Order Traversal II
题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题目原文 Given a binary tree, r ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Leet Code OJ 107. Binary Tree Level Order Traversal II [Difficulty: Easy]
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 【LeetCode从零单排】No102 Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
最新文章
- vue-cli脚手架(框架)
- html复选框位置,html – 对齐复选框和标签
- Python数据可视化2.6 一些最好的可视化实践
- MapReduce关系代数运算
- mac 完全卸载android studio
- 介绍Dynamics 365的OrgDBOrgSettings工具
- ECCV 2020 论文大盘点-姿态估计与动作捕捉篇
- ECMAScript 6新特性介绍
- Bailian3195 最大公约数【数论】
- 统计学习基础:数据挖掘、推理和预测_百度零基础深度学习笔记(三) 波士顿房价预测...
- 小程序短视频项目———视频详情页面开发(二)
- SpringBoot实现本地、网络文件下载、zip压缩包批量下载
- 微信生成带参数二维码以及获取此二维码参数
- 118.Python修炼之路【123-前端-JQuery样式操作】2018.08.01
- 20180710使用gh
- 广州集体户口办理未婚证流程
- I DEA出现Spring配置错误:class path resource [.xml] cannot be opened because it does not exist
- 2022劳务员-岗位技能(劳务员)考试试题及答案
- 零距离接触阿里云时序时空数据库TSDB
- 代码审计(全文通读审计案例)
热门文章
- WordPress 5.0 换回老版”Classic Editor”经典编辑器教程
- VSCode摸鱼插件 — FreeWindow
- Linux环境变量的设置和查看
- Linux c 算法与数据结构--栈
- spring boot使用logback实现多环境日志配置
- char[]:strlen和sizeof的区别
- Taro+react开发(32) Please use the ‘new‘ operator, this DOM object constructor cannot be called as a fu
- 前端学习(3271):js中this的使用
- 前端学习(3185):ant-design的button介绍按钮属性
- [html] input元素size属性和width 的区别是什么?