每日一道算法题 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       8 阅读
  2. 每日算法 LCR 151. 彩灯装饰记录 III

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

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

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

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

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

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

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

最近更新

  1. 人工智能:改变我们日常生活的无形力量

    2024-07-10 07:20:02       0 阅读
  2. F - Palindromic Expression (abc363)

    2024-07-10 07:20:02       0 阅读
  3. iOS应用冷启动优化,可以做哪些事情

    2024-07-10 07:20:02       0 阅读
  4. MySQL学习之事务,锁机制

    2024-07-10 07:20:02       0 阅读
  5. 神经架构搜索:目标检测的未来

    2024-07-10 07:20:02       0 阅读
  6. Spring 系列

    2024-07-10 07:20:02       0 阅读
  7. 什么是CRISPR/Cas9?

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

热门阅读

  1. Ubuntu 添加so库搜索路径

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

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

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

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

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

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

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

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