力扣515. 在每个树行中找最大值(BFS,DFS)

news/2024/7/20 20:29:35 标签: 深度优先, leetcode, 宽度优先

Problem: 515. 在每个树行中找最大值

文章目录

  • 题目描述
  • 思路
  • 复杂度
  • Code

题目描述

在这里插入图片描述在这里插入图片描述

思路

思路1:BFS

套用BFS模板,直接在遍历树的某一层时将当前层的最大值存入数组中

思路2:DFS

回溯思想,在递归时不断更新可选列表(根据当前树的层数,也可以抽象看作是回溯思想中的决策阶段)

复杂度

思路1:
时间复杂度:

O ( n ) O(n) O(n);其中 n n n为二叉树节点的个数

空间复杂度:

O ( n ) O(n) O(n)

思路2:
时间复杂度:

O ( n ) O(n) O(n)

空间复杂度:

O ( h e i g h t ) O(height) O(height);其中 h e i g h t height height为二叉树的高度

Code

思路1:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    /**
     * Get the largest values of each level
     *
     * @param root The root of a binary tree
     * @return vector<int>
     */
    vector<int> largestValues(TreeNode* root) {
        if (root == nullptr) {
            return{};
        }
        return levelMaxNumber(root);
    }

    /**
     * BFS
     * 
     * @param root The root of a binary tree
     * @return vector<int>
     */
    vector<int> levelMaxNumber(TreeNode* root) {
        vector<int>res;
        queue<TreeNode*> queue;
        int depth = 0;
        queue.push(root);
        res.push_back(root -> val);
        while (!queue.empty()) {
            int levelMaxNum = INT_MIN;
            int curLevelSize = queue.size();
            for (int i = 0; i < curLevelSize; ++i) {
                TreeNode* curLevelNode = queue.front();
                queue.pop();
                if (curLevelNode -> left != nullptr) {
                    TreeNode* nextLeftNode = curLevelNode -> left;
                    queue.push(nextLeftNode);
                    levelMaxNum = max(nextLeftNode -> val, levelMaxNum);
                }
                if (curLevelNode -> right != nullptr) {
                    TreeNode* nextRightNode = curLevelNode -> right;
                    queue.push(nextRightNode);
                    levelMaxNum = max(nextRightNode -> val, levelMaxNum);
                }
            }
            if (!queue.empty()) {
                res.push_back(levelMaxNum);
            }
        }
        return res;
    }
};

思路2:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    /**
     * Get the largest values of each level
     *
     * @param root The root of a binary tree
     * @return vector<int>
     */
    vector<int> largestValues(TreeNode *root) {
        vector<int> res;
        if (root == nullptr) {
            return {};
        }
        dfs(res, root, 0);
        return res;
    }

    /**
     * DFS
     *
     * @param res Result set
     * @param root The root of a binary tree
     * @param curHeight The current height of a binary tree
     */
    void dfs(vector<int> &res, TreeNode *root, int curHeight) {
        if (curHeight == res.size()) {
            res.push_back(root->val);
        } else {
            res[curHeight] = max(res[curHeight], root->val);
        }
        if (root->left) {
            dfs(res, root->left, curHeight + 1);
        }
        if (root->right) {
            dfs(res, root->right, curHeight + 1);
        }
    }
};

http://www.niftyadmin.cn/n/5412351.html

相关文章

【AIGC】Stable Diffusion的建模思想、训练预测方式快速

在这篇博客中&#xff0c;将会用机器学习入门级描述&#xff0c;来介绍Stable Diffusion的关键原理。目前&#xff0c;网络上的使用教程非常多&#xff0c;本篇中不会介绍如何部署、使用或者微调SD模型。也会尽量精简语言&#xff0c;无公式推导&#xff0c;旨在理解思想。让有…

直接修改zynq petalinux编译出来的rootfs.cpio.gz文件内容

xilinx zynq petalinux 默认编译打包出的SPI flash烧写启动文件是BOOT.BIN&#xff0c;然而每次需要修改rootfs内的文件时都要重新build rootfs 然后再 package一次才能生成新的BOOT.bin文件&#xff0c;地球人都知道petalinux编译一次是很耗时间的&#xff0c;那么有没有什么简…

Spring——Bean的作用域

bean的作用域 Bean Scope Scope说明singleton&#xff08;默认情况下&#xff09;为每个Spring IoC容器将单个Bean定义的Scope扩大到单个对象实例。prototype将单个Bean定义的Scope扩大到任何数量的对象实例。session将单个Bean定义的Scope扩大到一个HTTP Session 的生命周期…

UKP3D和AutoPDMS选择支吊架点修改颜色的方法

Q:&#xff08;UKP3D&#xff09;咱们的管道和管道上的支吊架能用不同颜色显示吗&#xff1f; (北京老用户反馈&#xff1a;管道支吊架和支吊架点同一个颜色的话&#xff0c;看不清楚&#xff1b;一些管道上的支吊架点特别多&#xff0c;有时候100个点&#xff0c;要是每个点选…

springboot2.x升级到3.x注意事项汇总

随着Spring Framework URL解析不当漏洞&#xff08;CVE-2024-22243&#xff09;的发布&#xff0c;springboot升级到3.x迫在眉睫&#xff0c;2.x的版本官方并未提供该漏洞的修复版本&#xff0c;后续应该也不会再发布新的版本。 2.x升级到3.x是一次大的跨越&#xff0c;接下来…

【Mybatis】批量映射优化 分页插件PageHelper 逆向工程插件MybatisX

文章目录 一、Mapper批量映射优化二、插件和分页插件PageHelper2.1 插件机制和PageHelper插件介绍2.2 PageHelper插件使用 三、逆向工程和MybatisX插件3.1 ORM思维介绍3.2 逆向工程3.3 逆向工程插件MyBatisX使用 总结 一、Mapper批量映射优化 需求: Mapper 配置文件很多时&…

Windows 内核和 Linux 内核谁更复杂?

Windows 内核和 Linux 内核谁更复杂? 在开始前我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「Linux的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&…

react 分步表单中使用useEffect来更新表单值的问题

问题背景&#xff1a;我在完成一个分步表单的功能的时候&#xff0c;在进行点击下一步的时候&#xff0c;会通过useEffect 来监听下一步或者上一步的动作&#xff0c;进行表单赋值&#xff0c;我使用 useEffect(() > {setFieldsValue(formValues);}, [stepNum]) 直接赋值的…