当前位置: 首页 > article >正文

C语言 | Leetcode C语言题解之第515题在每个树行中找最大值

题目:

题解:

#define MAX_NODE_SIZE 10001
#define MAX(a, b) ((a) > (b) ? (a) : (b))

int* largestValues(struct TreeNode* root, int* returnSize) {
    if (!root) {
        *returnSize = 0;
        return NULL;
    }
    int *res = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
    int pos = 0;
    struct TreeNode **queue = (struct TreeNode *)malloc(sizeof(struct TreeNode *) * MAX_NODE_SIZE);
    int head = 0, tail = 0;
    queue[tail++] = root;
    while (head != tail) {
        int len = tail - head;
        int maxVal = INT_MIN;
        while (len > 0) {
            len--;
            struct TreeNode *node = queue[head++];
            maxVal = MAX(maxVal, node->val);
            if (node->left) {
                queue[tail++] = node->left;
            }
            if (node->right) {
                queue[tail++] = node->right;
            }
        }
        res[pos++] = maxVal;
    }
    *returnSize = pos;
    free(queue);
    return res;
}

http://www.kler.cn/news/368340.html

相关文章:

  • C++ | Leetcode C++题解之第516题最长回文子序列
  • Java | Leetcode Java题解之第513题找树左下角的值
  • 英伟达GPU算力【自用】
  • 虚拟光驱软件 PowerISO v8.7.0 中文激活版
  • 初识Linux · 动静态库(incomplete)
  • Linux -- 共享内存(2)
  • 《Knowledge Graph Enhanced Multimodal Transformer for Image-Text Retrieval》中文校对版
  • NtripShare Cloud平台之CORS服务之基准站RTCM坐标编辑
  • Apache paino DML操作实战
  • Python数据分析——Numpy
  • Git快速上手
  • Java实现 itext PDF文件打印水印(文字和图片水印)
  • Vue前端开发:双向数据绑定之v-model与修饰符
  • 基于STM32的水产品运输监测系统设计与实现
  • 湖南(满意度调查)源点咨询 市场调研中定量调研方式的运用技巧
  • 使用ceph-csi把ceph-fs做为k8s的storageclass使用
  • 基于vite和vue3、 eslint、prettier、stylelint、husky规范
  • Python实现贝叶斯优化器(Bayes_opt)优化简单循环神经网络分类模型(SimpleRNN分类算法)项目实战
  • count(1)、count(*)、count(主键)、count(字段)区别
  • openlayers 封装加载本地geojson数据 - vue3
  • git stash和git stash pop
  • Linux之nginx离线安装
  • 图文详解ChatGPT-o1完成论文写作的全流程
  • android openGL ES详解——缓冲区VBO/VAO/EBO/FBO/离屏渲染
  • 使用Node.js和Express构建RESTful API
  • 机器学习介绍