考察点

  • 广度优先遍历--层次遍历
  • STL内容器的用法

广度优先遍历的时候,首先应该想到的就是借助于队列。还需要在遍历下一层之前保存当前层节点的数量

代码很简单:

class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode* root) {
vector<vector<int> >vec;
if(root==NULL){
return vec;
}
queue <TreeNode *>qu;
qu.push(root);
int count=;//保存每一层的节点数量 while(!qu.empty()){
vector <int> v;
while(count>=){
TreeNode *node; node=qu.front();
qu.pop();
v.push_back(node->val);
count--;
if(node->left!=NULL){
qu.push(node->left);
}
if(node->right!=NULL){
qu.push(node->right);
}
}
vec.insert(vec.begin(),v);
count=qu.size();
} }
};

Binary Tree Level Order Traversal II --leetcode C++的更多相关文章

  1. Binary Tree Level Order Traversal II——LeetCode

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

  2. Binary Tree Level Order Traversal II [LeetCode]

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

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

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

  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. 102/107. Binary Tree Level Order Traversal/II

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

随机推荐

  1. Java随机数生成原理--转稿

    1.Math库里的static(静态)方法random() 该方法的作用是产生0到1之间(包括0,但不包括1)的一个double值. double rand = Math.random(); 2.通过 ...

  2. Hibernate学习之hibernate执行顺序

    Hibernate 执行的顺序如下:  (1) 生成一个事务的对象,并标记当前的 Session 处于事务状态(注:此时并未启动数据库级事务).  (2) 应用使用 s.save 保存对象,这个时候  ...

  3. 百度editor调用【图片上传阿里云】

    百度editor调用简单,但是图片和文件上传阿里云就有点难度了.下面我详细说一下. 百度富文本编辑器下载地址:http://ueditor.baidu.com/website/download.htm ...

  4. 5.7.2.4 random() 方法

    Math.random()方法返回大于等于0小于1的一个随机数.对于某些站点来说,这个方法非常实用,因为可以利用它来随机显示一些名人名言和新闻事件.套用下面的公式,就可以利用Math.random() ...

  5. js浮点数精度问题

    大多数语言在处理浮点数的时候都会遇到精度问题,但是在JS里似乎特别严重,来看一个例子 alert(45.6*13); 结果居然是592.800000000001,当然加法之类的也会有这个问题 那这是j ...

  6. C++から広がり

    泛型编程(Generic Programming)最初提出时的动机很简单直接:发明一种语言机制,能够帮助实现一个通用的标准容器库.所谓通用的标准容器库,就是要能够做到,比如用一个List类存放所有可能 ...

  7. 用Cython加速Python程序以及包装C程序简单测试

    用Cython加速Python程序 我没有拼错,就是Cython,C+Python=Cython! 我们来看看Cython的威力,先运行下边的程序: import time def fib(n): i ...

  8. 每天一个小算法(4)----在O(1)时间删除指定结点

    O(1)时间内删除结点的思路只能是复制该结点下一个结点的数据,然后删除该结点的下一个结点,来等效删除此结点. 需要注意的地方是删除头结点和尾结点的处理. #include <stdio.h> ...

  9. MFC窗口实现最小化到托盘 右键菜单和还原

    //.h文件 void toTray();//最小化到托盘 void DeleteTray();//删除托盘图标 afx_msg LRESULT OnShowTask(WPARAM wParam,LP ...

  10. php 前端获取数据

    <pre name="code" class="python"><!doctype html> <html lang=" ...