目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

示例:

给定二叉树 [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)相关推荐

  1. LeetCode 107. Binary Tree Level Order Traversal II

    LeetCode 107. Binary Tree Level Order Traversal II Solution1:我的答案 比102那道题多了一行代码... /*** Definition f ...

  2. 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 ...

  3. Binary Tree Level Order Traversal II 解题思路

    思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...

  4. 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 ...

  5. Leetcode | 107. Binary Tree Level Order Traversal II

    题目:二叉树的层次遍历 II 1. 代码①:深度优先搜索(链接) /*** Definition for a binary tree node.* struct TreeNode {* int val ...

  6. [Leetcode]@python 107. Binary Tree Level Order Traversal II

    题目链接 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题目原文 Given a binary tree, r ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

最新文章

  1. vue-cli脚手架(框架)
  2. html复选框位置,html – 对齐复选框和标签
  3. Python数据可视化2.6 一些最好的可视化实践
  4. MapReduce关系代数运算
  5. mac 完全卸载android studio
  6. 介绍Dynamics 365的OrgDBOrgSettings工具
  7. ECCV 2020 论文大盘点-姿态估计与动作捕捉篇
  8. ECMAScript 6新特性介绍
  9. Bailian3195 最大公约数【数论】
  10. 统计学习基础:数据挖掘、推理和预测_百度零基础深度学习笔记(三) 波士顿房价预测...
  11. 小程序短视频项目———视频详情页面开发(二)
  12. SpringBoot实现本地、网络文件下载、zip压缩包批量下载
  13. 微信生成带参数二维码以及获取此二维码参数
  14. 118.Python修炼之路【123-前端-JQuery样式操作】2018.08.01
  15. 20180710使用gh
  16. 广州集体户口办理未婚证流程
  17. I DEA出现Spring配置错误:class path resource [.xml] cannot be opened because it does not exist
  18. 2022劳务员-岗位技能(劳务员)考试试题及答案
  19. 零距离接触阿里云时序时空数据库TSDB
  20. 代码审计(全文通读审计案例)

热门文章

  1. WordPress 5.0 换回老版”Classic Editor”经典编辑器教程
  2. VSCode摸鱼插件 — FreeWindow
  3. Linux环境变量的设置和查看
  4. Linux c 算法与数据结构--栈
  5. spring boot使用logback实现多环境日志配置
  6. char[]:strlen和sizeof的区别
  7. Taro+react开发(32) Please use the ‘new‘ operator, this DOM object constructor cannot be called as a fu
  8. 前端学习(3271):js中this的使用
  9. 前端学习(3185):ant-design的button介绍按钮属性
  10. [html] input元素size属性和width 的区别是什么?