主页 Binary Tree Level Order Traversal
Post
Cancel

Binary Tree Level Order Traversal

Preface

This article contains strong personal opinions. If you find it uncomfortable to read, please close it immediately. This article is for personal learning records only. You are welcome to reproduce or share it within the scope of the license agreement. Please respect copyright and keep the original link. Thank you for your understanding and cooperation. If you find this site helpful, you can subscribe via RSS. Thank you for your support!

Problem

Given the root node root of a binary tree, return its level order traversal (i.e., visit all nodes level by level, from left to right).

Example 1

1
2
3
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]

Example 2

1
2
输入:root = [1]
输出:[[1]]

Example 3

1
2
输入:root = []
输出:[]

Implementation Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
  
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root == nullptr) { return ret; }
        queue <TreeNode *> q;
        q.push(root);
        while(!q.empty()) {
            int levelSize = q.size();
            ret.push_back(vector<int>());
            for (int i = 1; i <= levelSize; ++i) {
                auto node = q.front();
                q.pop();
                ret.back().push_back(node->val);
                if (node->left) {
                    q.push(node->left);
                }
                if (node->right) {
                    q.push(node->right);
                }
            }
        }
        return ret;
    }
};

102. Binary Tree Level Order Traversal
Quoted from codetop

该博客文章由作者通过 CC BY 4.0 进行授权。