力扣199. 二叉树的右视图(DFS,BFS)

Problem: 199. 二叉树的右视图

文章目录

题目描述

思路

无论是DFS 还是BFS 我们都要思考到达二叉树的每一层(或者每一层中的每一个节点)时,我们都该如何按题目要求做出对应得处理!!!在本体中我们主要是:

1.当右子树与左子树等高或者右子树高于左子树时,我们只添加每一个右子树得右节点到结果集(根节点得左子树整个都去除)

2.当左子树高于右子树时,我们将等高得部分按1中处理,左子树高出右子树得部分,再将其右子树得右节点添加到结果集

解题方法

思路1:DFS

1.创建unordered_map<int, int> rightmostValueAtDepth;记录每一层应该添加到右视图 的节点,int maxDepth = -1;记录并维护当前的最大深度,stack<TreeNode > nodeStack;用于DFS过程中的存储节点,stack depthStack;用于同时记录DFS过程中的树的深度;
2.将根节点添加到nodeStack中,while循环遍历(循环退出条件为nodeStack为空),每次弹出nodeStack和depthStack中的栈顶元素
node
depth
,若此时
node不为空
则更新最大深度,同时
若rightmostValueAtDepth[depth]不存在则添加到rightmostValueAtDepth中 *;并且将node -> left;node -> right;depth + 1;depth + 1 分别添加到对应的nodeStack和depthStack栈

3.最后将rightmostValueAtDepth中的值添加到一个一维数组中即可

思路2:BFS

大体实现直接套用BFS代码的模板书写即可,具体解释下面代码实现中的两个点

1.由于常规的BFS 模板均是先添加左子树节点到队列,再添加右子树节点到队列 所以我们可以利用数组元素可以覆盖 的特性在每次添加节点到队列时,也将该节点值放入一个空间大小为1的数组temp中 ,这样操作后无论是思路中1、2哪种情况,都能保证最后添加到结果集中的节点值是复合题目右视图 这个定义;

2.按常规BFS 代码的实现(或者说就是按下面代码前面部分的操作)会导致最后一个被写到temp数组中的元素会添加两次到最终的结果集中 ,所以要push_back一次!!!

复杂度

思路1、2均如下

时间复杂度:

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

空间复杂度:

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

Code

思路1:

cpp 复制代码
class Solution {
public:
    /**
     * DFS
     *
     * @param root The root of a binary tree
     * @return vector<int>
     */
    vector<int> rightSideView(TreeNode *root) {
        if (root == nullptr) {
            return {};
        }
        unordered_map<int, int> rightmostValueAtDepth;
        int maxDepth = -1;
        stack<TreeNode *> nodeStack;
        stack<int> depthStack;
        nodeStack.push(root);
        depthStack.push(0);

        while (!nodeStack.empty()) {
            TreeNode *node = nodeStack.top();
            nodeStack.pop();
            int depth = depthStack.top();
            depthStack.pop();

            if (node != nullptr) {
                //Maintain the maximum depth of a binary tree
                maxDepth = max(maxDepth, depth);
                //If not in the map collection, add it
                if (rightmostValueAtDepth.find(maxDepth) == rightmostValueAtDepth.end()) {
                    rightmostValueAtDepth[depth] = node->val;
                }
                nodeStack.push(node->left);
                nodeStack.push(node->right);
                depthStack.push(depth + 1);
                depthStack.push(depth + 1);
            }
        }
        vector<int> res;
        for (int i = 0; i < maxDepth; ++i) {
            res.push_back(rightmostValueAtDepth[i]);
        }
        return res;
    }
};

思路2:

cpp 复制代码
class Solution {
public:
    /**
     * BFS
     * 
     * @param root The root of a binary tree
     * @return vector<int>
     */
    vector<int> rightSideView(TreeNode* root) {
        if (root == nullptr) {
            return {};
        }
        if (root -> left == nullptr && root -> right == nullptr) {
            return {root -> val};
        }
        vector<int> res;
        vector<int> temp(1);
        queue<TreeNode*> queue;
        res.push_back(root -> val);
        queue.push(root);
        while (!queue.empty()) {
            int curLevelSize = queue.size();
            for (int i = 0; i < curLevelSize; ++i) {
                TreeNode* curLevelNode = queue.front();
                queue.pop();
                if (curLevelNode -> left != nullptr) {
                    temp[0] = curLevelNode -> left -> val;
                    queue.push(curLevelNode -> left);
                }
                if (curLevelNode -> right != nullptr) {
                    temp[0] = curLevelNode -> right -> val;
                    queue.push(curLevelNode -> right);
                }
            }
            res.push_back(temp[0]);
        }
        //Pop the last repetitive node
        res.pop_back();
        return res;
    }
};
相关推荐
Tanecious.2 小时前
LeetCode 876. 链表的中间结点
算法·leetcode·链表
Two_brushes.9 小时前
【算法】宽度优先遍历BFS
算法·leetcode·哈希算法·宽度优先
凌肖战19 小时前
力扣网编程55题:跳跃游戏之逆向思维
算法·leetcode
黑听人19 小时前
【力扣 简单 C】70. 爬楼梯
c语言·leetcode
ゞ 正在缓冲99%…21 小时前
leetcode918.环形子数组的最大和
数据结构·算法·leetcode·动态规划
Kaltistss21 小时前
98.验证二叉搜索树
算法·leetcode·职场和发展
许愿与你永世安宁1 天前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子1 天前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
YuTaoShao1 天前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
杰克尼1 天前
1. 两数之和 (leetcode)
数据结构·算法·leetcode