每日一道算法题 LCR 150. 彩灯装饰记录 II

题目

LCR 150. 彩灯装饰记录 II - 力扣(LeetCode)

Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def decorateRecord(self, root: Optional[TreeNode]) -> List[List[int]]:
        ans=[]
        if not root:
            return ans
        qu=[root]
        while qu:
            le=len(qu)
            row=[]
            for _ in range(le):
                cur=qu.pop(0)
                row.append(cur.val)
                if cur.left:
                    qu.append(cur.left)
                if cur.right:
                    qu.append(cur.right)
            ans.append(row)
        return ans

C++

/**
 * Definition for a binary tree node.
 * 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) {}
 * };
 */
 #include <queue>
class Solution {
public:
    vector<vector<int>> decorateRecord(TreeNode* root) 
    {
        vector<vector<int>> ans;
        queue<TreeNode*> qu;
        if(root!=NULL)  qu.push(root);

        while(!qu.empty())
        {
            int le=qu.size();
            vector<int> row;
            for(int _=0;_<le;_++)
            {
                TreeNode* cur=qu.front();
                qu.pop();
                row.push_back(cur->val);
                if(cur->left) qu.push(cur->left);
                if(cur->right) qu.push(cur->right);
            }
            ans.push_back(row);
        }

    return ans;
    }
};

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** decorateRecord(struct TreeNode* root, int* returnSize,int** returnColumnSizes) 
{
    if (root == NULL) {
        *returnSize = 0;
        return root;
    }

    struct TreeNode* qu[1100];
    struct TreeNode* cur;
    int front = 0, rear = 0, row = 0, col = 0, next_row_count = 0, cur_row_count = 1;
    int** ans = (int**)malloc(sizeof(int*) * 1100);
    *returnColumnSizes = (int*)malloc(sizeof(int) * 1100);
    qu[rear++] = root;  //入队
    ans[row] = (int*)malloc(sizeof(int) * cur_row_count);
    (*returnColumnSizes)[row] = cur_row_count;
    while (front < rear) {
        cur = qu[front++];  //出队
        ans[row][col++] = cur->val;
        if (cur->left) {
            qu[rear++] = cur->left;
            next_row_count++;
        }
        if (cur->right) {
            qu[rear++] = cur->right;
            next_row_count++;
        }
        cur_row_count--;
        if (cur_row_count == 0) {
            cur_row_count = next_row_count;
            next_row_count = 0;
            col = 0;
            row++;
            ans[row] = (int*)malloc(sizeof(int) * cur_row_count);
            (*returnColumnSizes)[row] = cur_row_count;
        }
    }
    *returnSize = row;
    return ans;
}

相关推荐

  1. 每日算法 LCR 150. 彩灯装饰记录 II

    2024-07-10 07:20:02       33 阅读
  2. 每日算法 LCR 151. 彩灯装饰记录 III

    2024-07-10 07:20:02       37 阅读
  3. 每日算法 1

    2024-07-10 07:20:02       61 阅读
  4. 每日算法 3(2023-12-11)

    2024-07-10 07:20:02       48 阅读
  5. 每日算法 14(2023-12-22)

    2024-07-10 07:20:02       47 阅读
  6. 每日算法 求最小公倍数

    2024-07-10 07:20:02       93 阅读
  7. 每日算法 204. 计数质数

    2024-07-10 07:20:02       31 阅读
  8. 每日算法 994. 腐烂的橘子

    2024-07-10 07:20:02       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 07:20:02       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 07:20:02       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 07:20:02       90 阅读
  4. Python语言-面向对象

    2024-07-10 07:20:02       98 阅读

热门阅读

  1. Ubuntu 添加so库搜索路径

    2024-07-10 07:20:02       33 阅读
  2. 文件格式是.pb应该怎么查看?

    2024-07-10 07:20:02       37 阅读
  3. 高考假期预习指南

    2024-07-10 07:20:02       34 阅读
  4. YOLOv5/v7 应用轻量级通用上采样算子CARAFE

    2024-07-10 07:20:02       29 阅读
  5. 探索Hash Router:构建单页应用的基石

    2024-07-10 07:20:02       27 阅读
  6. Django学习收尾

    2024-07-10 07:20:02       29 阅读
  7. Linux Vim全面教程

    2024-07-10 07:20:02       30 阅读
  8. 【Linux命令基础】vim的简介

    2024-07-10 07:20:02       28 阅读