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

给定一个二叉树,返回其按层次遍历的节点值。 (即zhu‘ceng‘de,从左到右访问)。
例如:
给定二叉树: [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果为:
[
  [3],
  [9,20],
  [15,7]
]
详见:https://leetcode.com/problems/binary-tree-level-order-traversal/description/

/**
 * 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>> res;
        if(!root)
        {
            return res;
        }
        queue<TreeNode*> que;
        que.push(root);
        vector<int> out;
        int i=1;
        while(!que.empty())
        {
            root=que.front();
            que.pop();
            --i;
            out.push_back(root->val);
            if(root->left)
            {
                que.push(root->left);
            }
            if(root->right)
            {
                que.push(root->right);
            }
            if(i==0)
            {
                i=que.size();
                res.push_back(out);
                out.clear();
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8718154.html

时间: 2024-12-28 10:09:09

102 Binary Tree Level Order Traversal 二叉树的层次遍历的相关文章

leetcode 题解: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] ] co

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

二叉树的层次遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<in

lintcode 中等题:binary tree level order traversal 二叉树的层数遍历

题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / 9 20 / 15 7 返回他的分层遍历结果: [ [3], [9,20], [15,7] ] 挑战 挑战1:只使用一个队列去实现它 挑战2:用DFS算法来做 解题  队列很容易,先加入,然后取出来的同时加入左右孩子节点 在剑指offer中有个题目和这个很类似,其只是层次遍历二叉树,没有要求把每层的节点单独放在一起的. 上面说的规律:每一次打印一

刷题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

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

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 re

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

(二叉树 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] ]