• 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:
    /**
     * 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
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
  • 相关阅读:
    外包干了2个月,技术退步明显.......
    VirtualBox网络配置
    优雅使用前端枚举Enum,符合国标的那种!
    CPU密集型、IO密集型
    一文搞定基因型数据清洗
    解决OpenCV捕捉USB摄像头时抓帧失败的问题
    华为机试真题 Java 实现【最多团队】
    2.KDTree相关
    实践数据湖iceberg 第三十八课 spark sql, Procedures语法进行数据治理(小文件合并,清理快照)
    C++面试八股文:了解位运算吗?
  • 原文地址:https://blog.csdn.net/navicheung/article/details/132925344