题目

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:

Given binary tree {3,9,20,#,#,15,7},



return its bottom-up level order traversal as:

分析

LeetCode(103) Binary Tree Zigzag Level Order Traversal 以及 LeetCode(102) Binary Tree Level Order Traversal 本质相同的题目,只不过灵活调整结果返回格式罢了。

AC代码

/**
* 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) {
//层次遍历,分层存储
if (!root)
return vector<vector<int> >(); vector<vector<int> > ret; //定义两个队列,一个存储所有的父节点,另一个存储他们的子节点也就是子层
queue<TreeNode *> parents; parents.push(root); while (!parents.empty())
{
//存储当前层的遍历结果
vector<int> tmp;
//定义队列存储他们的子节点也就是子层
queue<TreeNode *> childs;
while (!parents.empty())
{
TreeNode *node = parents.front();
tmp.push_back(node->val);
//弹出当前父节点
parents.pop(); if (node->left)
childs.push(node->left); if (node->right)
childs.push(node->right);
}
//存储当前层的遍历结果
ret.push_back(tmp);
//遍历下一层
parents = childs;
}
//反转遍历结果 由下向上存储
reverse(ret.begin(), ret.end());
return ret;
}
};

GitHub测试程序源码

LeetCode(107) Binary Tree Level Order Traversal II的更多相关文章

  1. LeetCode(26)-Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  2. LeetCode(102) Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  3. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  4. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  5. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  6. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  7. LeetCode_107. Binary Tree Level Order Traversal II

    107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...

  8. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  9. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

随机推荐

  1. C# 获取当前ip

    1.获取局域网ip IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址 string ip=ipAd ...

  2. Serega and Fun Codeforces - 455D || queue

    https://codeforces.com/problemset/problem/455/D 其实方法很多,然而当初一个也想不到... 1.分块,块内用链表维护 修改[l,r]就当成删除第r个元素, ...

  3. hdu2475Box(splay树形转线性)

    链接 推荐一篇帖子 http://blog.csdn.net/lyhypacm/article/details/6734748 这题暴力不可行主要是因为这颗树可能极度不平衡,不能用并查集是不能路径压缩 ...

  4. 【javascript】操作给定的二叉树,将其变换为源二叉树的镜像。

    操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 ...

  5. arcgis jsapi接口入门系列(1):地图

    地图相关 //地图相关demo mapFun: function () { //获取地图中心点 let center = this.mapView.center; //地图中心点坐标(同地图坐标系) ...

  6. Git常用命令的使用方法

    推荐一个比较好的GIT的教学地址,廖雪峰老师的git教程! 这里简述Git常用命令的使用方法: 一.初始化git 右键进入 Git Bash 1.建立身份信息 git config --global ...

  7. sublime快捷键mark

    Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...

  8. Azure 项目构建 - 用 Azure 认知服务在微信公众号上搭建智能会务系统

    通过完整流程详细介绍了如何在Azure平台上快速搭建基于微信公众号的智慧云会务管理系统. 此系列的全部课程 https://school.azure.cn/curriculums/11 立即访问htt ...

  9. C#背景图片自适应

    1.选中窗体修改属性 2.在load添加代码 private void Form1_Load(object sender, EventArgs e) { this.BackgroundImageLay ...

  10. python基础一 day14 生成器函数进阶

    def generator(): print(123) content = yield 1 print('=======',content) print(456) arg = yield 2 '''' ...