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;
}
相关推荐
为什么这亚子1 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
1 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
幸运超级加倍~2 小时前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
yannan201903132 小时前
【算法】(Python)动态规划
python·算法·动态规划
埃菲尔铁塔_CV算法2 小时前
人工智能图像算法:开启视觉新时代的钥匙
人工智能·算法
EasyCVR2 小时前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
linsa_pursuer2 小时前
快乐数算法
算法·leetcode·职场和发展
小芒果_012 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
qq_434085902 小时前
Day 52 || 739. 每日温度 、 496.下一个更大元素 I 、503.下一个更大元素II
算法