一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源: https://leetcode.com/problems/binary-tree-level-order-traversal/

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

For example:

Given binary tree [3,9,20,null,null,15,7],

3

/ \

9 20

/ \

15 7

return its level order traversal as:

[

[3],

[9,20],

[15,7]

]

(二)解题

本题大意:给定一个二叉树,按层的顺序输出各层元素

解题思路:每次将该层元素压入一个queue,然后依次处理这个queue里的每个元素,并把它们的左右节点都压入另一个queue,一直这样处理,直到queue为空。

/**
 * 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>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root==NULL) return ret;
        queue<TreeNode*> que;//用来存放每层的节点
        que.push(root);//先压入根节点
        while(!que.empty())
        {
            vector<int> temp;
            queue<TreeNode*> tmpQue;//存放下一层queue
            while(!que.empty())
            {
                TreeNode* tempNode = que.front();//依次取出queue里的元素进行处理
                que.pop();
                temp.push_back(tempNode->val);//将值压入vector
                if(tempNode->left!=NULL) tmpQue.push(tempNode->left);//处理左节点
                if(tempNode->right!=NULL) tmpQue.push(tempNode->right);//处理右节点
            }
            ret.push_back(temp);//将每层的结果压入返回vector中
            que=tmpQue;.
        }
        return ret;
    }
};

【一天一道LeetCode】#102. Binary Tree Level Order Traversal的更多相关文章

  1. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

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

  3. leetcode 102 Binary Tree Level Order Traversal ----- java

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

  4. Java [Leetcode 102]Binary Tree Level Order Traversal

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

  5. Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

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

  6. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  7. Java for LeetCode 102 Binary Tree Level Order Traversal

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

  8. leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)

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

  9. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  10. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

随机推荐

  1. WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  2. Python3 JSON 数据解析

    JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. Python3 中可以使用 json 模块来对 JSON 数据进 ...

  3. Django URL (路由系统)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这段代码,对于那 ...

  4. 学习笔记:Zookeeper选举机制

    1.Zookeeper选举机制 Zookeeper虽然在配置文件中并没有指定master和slave 但是,zookeeper工作时,是有一个节点为leader,其他则为follower Leader ...

  5. hive中的NULL(hive空值处理)

    HIVE表中默认将NULL存为\N,可查看表的源文件(hadoop fs -cat或者hadoop fs -text),文件中存储大量\N, 这样造成浪费大量空间.而且用java.python直接进入 ...

  6. 物料REVISION控制

    --新增 INV_ITEM_REVISION_PUB.Create_Item_Revision ( p_api_version IN NUMBER , p_init_msg_list IN VARCH ...

  7. FORM界面批量处理-全选框实现

    全选框实现方法多种多样,这里只介绍两种 方法一:触发器式,优点程序简单,缺点颜色单调不突出 1.      在数据块和控制块上分别创建check box 2.      设置check box选中与为 ...

  8. OpenMP实现生产者消费者模型

    生产者消费者模型已经很古老了吧,最近写了个OpenMP版的此模型之实现,来分享下. 先说一下模型的大致做法是: 1.生产者需要取任务,生产产品. 2.消费者需要取产品,消费产品. 生产者在生产某个产品 ...

  9. activty栈管理

    题外话:我们有时在开发中,通常会有如下的需求:屏幕1-->屏幕2-->屏幕3-->屏幕4...,现在需要直接从屏幕4-->屏幕1,很多人会想到对activity进行管理得到对应 ...

  10. java创建对象详解和多态问题

    一. java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象的.  二.Test test = new Test(); 有人用上面的表达式来说明构造方法返回对象引用,这是明显 ...