【每日一题】LeetCode 199. 二叉树的右视图

每日一题,防止痴呆 = =

一、题目大意

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
在这里插入图片描述
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-right-side-view

二、题目思路以及AC代码

这道题思路不算是很难,感觉就是一个实现的问题。

说一下大致思路,首先明确大致的复杂度,因为要获取全图,应该要以O(n)作为基础考虑,其实这个问题化简一下,就是要你求每一行的最右边的数,这么一说就很明确了,BFS就可以。

当然,官方题解给了一种DFS的做法,思路就是我们在DFS的时候,每次都先取右子,那么,我们可以保证,在进入一个新的深度时的第一个元素,就是我们需要的右视图的元素,想想都比BFS复杂,我看代码也是比BFS复杂。

下面的代码同样我只写了自己思路的那个版本,DFS的版本是LeetCode的官方解答。

下面给出AC代码

BFS版本

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;

        if (root == NULL) return res;
        queue<TreeNode*> q;
        q.push(root);

        while (!q.empty()) {
            int right_bound = -1;
            int q_size = q.size();
            for (int i=0;i<q_size;i++) {
                TreeNode* tn = q.front(); q.pop();
                
                right_bound = tn->val;
                if (tn->left) q.push(tn->left);
                if (tn->right) q.push(tn->right);
            }

            res.push_back(right_bound);
        }

        return res;
    }
};

官方的DFS版本:

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        unordered_map<int, int> rightmostValueAtDepth;
        int max_depth = -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 != NULL) {
            	// 维护二叉树的最大深度
                max_depth = max(max_depth, depth);

                // 如果不存在对应深度的节点我们才插入
                if (rightmostValueAtDepth.find(depth) == rightmostValueAtDepth.end()) {
                    rightmostValueAtDepth[depth] =  node -> val;
                }

                nodeStack.push(node -> left);
                nodeStack.push(node -> right);
                depthStack.push(depth + 1);
                depthStack.push(depth + 1);
            }
        }

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

        return rightView;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/binary-tree-right-side-view/solution/er-cha-shu-de-you-shi-tu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

今天的每日一题好像有点水,看看之后有兴趣再搞一道 ^_^!

如果有问题,欢迎大家指正!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值