[leetcode]binary-tree-right-side-view 二叉树右视图

60 篇文章 0 订阅

. - 力扣(LeetCode)

/**
 * 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) {

        vector<int> rightView;
        if(!root)
            return rightView;
        queue<TreeNode*> nodeQueue;
        nodeQueue.push(root);


        while (!nodeQueue.empty()) {
            

            int size =  nodeQueue.size();
            for (int i = 0; i < size; ++i) {
                TreeNode* node = nodeQueue.front();
                nodeQueue.pop();

                if (i == size - 1) {
                    rightView.push_back(node -> val);
                }
                
                if(node->left) nodeQueue.push(node -> left);
                if(node->right) nodeQueue.push(node -> right);
                   
                
            }
        }


        return rightView;
    }
};

/**
 * 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) {
        unordered_map<int, int> rightmostValueAtDepth;
        int max_depth = -1;

        queue<TreeNode*> nodeQueue;
        queue<int> depthQueue;
        nodeQueue.push(root);
        depthQueue.push(0);

        while (!nodeQueue.empty()) {
            

            int size =  nodeQueue.size();
            for (int i = 0; i < size; ++i) {
                TreeNode* node = nodeQueue.front();nodeQueue.pop();
                int depth = depthQueue.front();depthQueue.pop();

                
                if (node != NULL) {
                    // 维护二叉树的最大深度
                    max_depth = max(max_depth, depth);

                    // 由于每一层最后一个访问到的节点才是我们要的答案,因此不断更新对应深度的信息即可
                    rightmostValueAtDepth[depth] =  node -> val;

                    nodeQueue.push(node -> left);
                    nodeQueue.push(node -> right);
                    depthQueue.push(depth + 1);
                    depthQueue.push(depth + 1);
                }
            }
        }

        vector<int> rightView;
        for (int depth = 0; depth <= max_depth; ++depth) {
            rightView.push_back(rightmostValueAtDepth[depth]);
        }

        return rightView;
    }
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值