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;
}
相关推荐
Navigator_Z26 分钟前
LeetCode //C - 1223. Dice Roll Simulation
c语言·算法·leetcode
171320330计算机毕设编程28 分钟前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
Tisfy32 分钟前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历
万物智能41 分钟前
设备树DTS-【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
后端·算法
代码不停1 小时前
子数组问题
java·算法
小欣加油1 小时前
Leetcode2058 找出临界点之间的最小和最大距离
数据结构·c++·算法·leetcode·职场和发展
Patrick在香港1 小时前
Python 拉取 C&SD 官方 API:香港 2022 年已跨过“超老龄线“,而抚养比正在爬回 1961
android·c语言·python·数据分析·时序数据库·数据可视化·香港
华华哥20221 小时前
《模型不玄学》第25章 双头模型:共享底座 + 两个分支
算法
Yfhonier1 小时前
6441. 特别的除法
c++·算法
华华哥20221 小时前
《模型不玄学》第5章 看懂你的数据:先认字段,再过门禁
算法