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&107-Binary Tree Level Order Traversal Ⅰ,Ⅱ

原题 102 107 解法 思路分析 与前面很多的题类似,为二叉树的广度优先遍历(BFS),借助一个队列,共N次迭代,N为树的层高,每一次迭代全部第i层的节点出队列,...

[leetcode]102. Binary Tree Level Order Traversal@Java解题报告

https://leetcode.com/problems/binary-tree-level-order-traversal/description/ Given a bina...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:102. Binary Tree Level Order Traversal
举报原因:
原因补充:

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