(二叉树 BFS) 102. 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]
]

-----------------------------------------------------------------------------就是遍历二叉树的的每一层,并且把每一层的数加入到一个数组,返回这个二维数组。

C++代码:
/**
 * 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) {
        queue<TreeNode*> q;
        vector<int> vec;
        vector<vector<int> > vecs;
        if(!root){
            return vecs;   //这个vecs指的是空数组。
        }
        q.push(root);
        while(!q.empty()){
            vec.clear();
            for(int i = q.size();i>0;i--){
                auto t = q.front();
                q.pop();
                vec.push_back(t->val);
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            vecs.push_back(vec);
        }
        return vecs;
    }
};

原文地址:https://www.cnblogs.com/Weixu-Liu/p/10721261.html

时间: 2024-10-15 11:19:30

(二叉树 BFS) 102. Binary Tree Level Order Traversal的相关文章

刷题102. Binary Tree Level Order Traversal

一.题目说明 题目102. Binary Tree Level Order Traversal,给一个二叉树,返回按层遍历的各节点,每层返回一个数组. 二.我的解答 这个题目是普通的二叉树按层遍历,没什么难度.代码如下: class Solution{ public: vector<vector<int>> levelOrder(TreeNode* root){ vector<vector<int>> r; TreeNode* p; if(root ==NU

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

leetcode—102 Binary Tree Level Order Traversal(二叉树的层次遍历)

Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478 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,#,#,15,7},

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

102. Binary Tree Level Order Traversal (Tree, Queue; BFS)

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

【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, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 提示

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, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] confu

[leedcode 102] 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,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,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, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 解题思路: