LeetCode //C - 102. Binary Tree Level Order Traversal

102. Binary Tree Level Order Traversal

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

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • -1000 <= Node.val <= 1000

From: LeetCode

Link: 102. Binary Tree Level Order Traversal


Solution:

Ideas:
  1. Create a queue to hold the nodes of the tree.
  2. Enqueue the root node to the queue.
  3. While the queue is not empty:
  • Calculate the number of nodes at the current level.
  • Allocate memory for these nodes.
  • For each node at the current level:
    • Dequeue the node from the queue.
    • Add the node's value to the result.
    • Enqueue the node's left child (if it exists).
    • Enqueue the node's right child (if it exists).
  1. Return the result.
Code:
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
    if (!root) {
        *returnSize = 0;
        return NULL;
    }

    // Create a queue for BFS
    struct TreeNode** queue = malloc(2000 * sizeof(struct TreeNode*));
    int front = 0, rear = 0;

    // Enqueue root
    queue[rear++] = root;

    int** result = malloc(2000 * sizeof(int*));
    *returnColumnSizes = malloc(2000 * sizeof(int));
    *returnSize = 0;

    while (front < rear) {
        // Number of nodes at current level
        int levelSize = rear - front;
        (*returnColumnSizes)[*returnSize] = levelSize;

        // Allocate memory for nodes of this level
        result[*returnSize] = malloc(levelSize * sizeof(int));

        for (int i = 0; i < levelSize; i++) {
            // Dequeue node
            struct TreeNode* current = queue[front++];

            // Add node's value to result
            result[*returnSize][i] = current->val;

            // Enqueue left child
            if (current->left) {
                queue[rear++] = current->left;
            }
            
            // Enqueue right child
            if (current->right) {
                queue[rear++] = current->right;
            }
        }
        (*returnSize)++;
    }
    free(queue);
    return result;
}
相关推荐
FJW0208146 分钟前
Python排序算法
python·算法·排序算法
钮钴禄·爱因斯晨8 分钟前
机器学习(二):KNN算法简介及API介绍(分类、回归)
人工智能·算法·机器学习·分类·回归
如此这般英俊10 分钟前
第八章-排序
数据结构·算法·排序算法
源代码•宸12 分钟前
Leetcode—146. LRU 缓存【中等】(哈希表+双向链表)
后端·算法·leetcode·缓存·面试·golang·lru
郭涤生21 分钟前
AWB算法基础理解
人工智能·算法·计算机视觉
承渊政道22 分钟前
C++学习之旅【C++Vector类介绍—入门指南与核心概念解析】
c语言·开发语言·c++·学习·visual studio
hetao173383723 分钟前
2026-01-21~22 hetao1733837 的刷题笔记
c++·笔记·算法
Hcoco_me25 分钟前
大模型面试题91:合并访存是什么?原理是什么?
人工智能·深度学习·算法·机器学习·vllm
2501_9011478328 分钟前
零钱兑换——动态规划与高性能优化学习笔记
学习·算法·面试·职场和发展·性能优化·动态规划·求职招聘
狐572 小时前
2026-01-22-LeetCode刷题笔记-3507-移除最小数对使数组有序I
笔记·leetcode