102. Binary Tree Level Order Traversal

原创 2016年08月30日 18:39:00

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

/**二叉树的bfs和队列个数,可分层处理到每一层的的节点。
 * 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> > outer;
        vector<int> inner;
        queue<TreeNode*> q;
        if (root)
            q.push(root);
        else
            return outer;
        while (!q.empty())
        {
            int size=q.size();//当前队列的个数,用来将当前层的节点全从队列中移除。
            inner.clear();
            while (size-->0)
            {
                TreeNode* t=q.front();
                q.pop();
                inner.push_back(t->val);
                if (t->left)
                   q.push(t->left);
                if (t->right)
                   q.push(t->right);
            }
            outer.push_back(inner);
        }
        return outer;
    }
  
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

【LeetCode】102. Binary Tree Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/pro...

Leetcode_102_Binary Tree Level Order Traversal

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41929059 Given a binary tree, ...

[leetcode-二叉树层次遍历并统计每层节点数]--102. Binary Tree Level Order Traversal

Question 102. Binary Tree Level Order Traversal Given a binary tree, return the level order traver...

[LeetCode]102.Binary Tree Level Order Traversal

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

LeetCode 102. Binary Tree Level Order Traversal 树的广度优先遍历 辅助数据结构队列

Binary Tree Level Order Traversal 题意 思路 代码 结果102. Binary Tree Level Order Traversal Given a binary...

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

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

102. Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of i...

Leetcode-102. Binary Tree Level Order Traversal

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...

102. Binary Tree Level Order Traversal

题目:Binary Tree Level Order Traversal题目叙述原题链接:https://leetcode.com/problems/binary-tree-level-order-t...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)