力扣199. 二叉树的右视图

广度优先搜索

  • 思路:
    • 使用广度优先搜索,那么每层最后一次遍历的节点就是最右视图的节点;
    • 广度优先搜索模板:
      • std::queue<TreeNode*> nq;
        nq.push(root);
        
        while (!nq.empty()) {
            int levelSize = nq.size();
                    
            // deal with every layer
            for (int idx = 0; idx < levelSize; ++idx) {
                auto node = nq.front();
                nq.pop();
        
                
                /// do something
                // if (idx == levelSize - 1) {
                //     result.push_back(node->val);
                // }
        
                if (node->left) {
                    nq.push(node->left);
                }
                if (node->right) {
                    nq.push(node->right);
                }
            }
        }

  • 完整代码:
/**
 * 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:
    vector<int> rightSideView(TreeNode* root) {
        std::vector<int> result;
        if (root == nullptr) {
            return result;
        }

        std::queue<TreeNode*> nq;
        nq.push(root);

        while (!nq.empty()) {
            int levelSize = nq.size();
            
            for (int idx = 0; idx < levelSize; ++idx) {
                auto node = nq.front();
                nq.pop();

                if (idx == levelSize - 1) {
                    result.push_back(node->val);
                }

                if (node->left) {
                    nq.push(node->left);
                }
                if (node->right) {
                    nq.push(node->right);
                }
            }
        }

        return result;
    }
};

相关推荐

  1. 199. 视图

    2023-12-08 06:36:04       55 阅读
  2. 100】199.视图

    2023-12-08 06:36:04       62 阅读
  3. 记录)199.视图

    2023-12-08 06:36:04       44 阅读
  4. 视图-

    2023-12-08 06:36:04       26 阅读
  5. 199_视图

    2023-12-08 06:36:04       49 阅读

最近更新

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

    2023-12-08 06:36:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 06:36:04       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 06:36:04       82 阅读
  4. Python语言-面向对象

    2023-12-08 06:36:04       91 阅读

热门阅读

  1. PostgreSQL 主键和唯一键的区别

    2023-12-08 06:36:04       55 阅读
  2. 安全快速地删除 MySQL 大表数据并释放空间

    2023-12-08 06:36:04       58 阅读
  3. excel xla文件怎么导入到excel

    2023-12-08 06:36:04       63 阅读
  4. 我的创作纪念日

    2023-12-08 06:36:04       70 阅读
  5. http状态码

    2023-12-08 06:36:04       61 阅读
  6. 单元测试Nunit的几种断言

    2023-12-08 06:36:04       54 阅读
  7. Hibernate更新多实体对象的坑

    2023-12-08 06:36:04       57 阅读
  8. BGP/Border Gateway Protocol

    2023-12-08 06:36:04       54 阅读
  9. Flask template中使用iframe

    2023-12-08 06:36:04       60 阅读
  10. Go入门:探索编程的奇妙世界

    2023-12-08 06:36:04       57 阅读
  11. 解析5种常用的Python设计模式

    2023-12-08 06:36:04       50 阅读