leetcode-javascript icon indicating copy to clipboard operation
leetcode-javascript copied to clipboard

LeetCode 199. 二叉树的右视图

Open Chocolate1999 opened this issue 4 years ago • 0 comments

仰望星空的人,不应该被嘲笑

题目描述

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

示例:

输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-right-side-view 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路

DFS,每一层只能取一个元素,那么我们搜的时候优先考虑右孩子即可。 参考 shetia 大佬图解

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function(root) {
    if(!root) return [];
    let res = [];
    let dfs = (step,root) => {
        if(res.length === step){
            res.push(root.val);
        }
        // 优先遍历右孩子,再遍历左孩子
        root.right && dfs(step+1,root.right);
        root.left && dfs(step+1,root.left);
    }
    dfs(0,root);
    return res;
};

解法二

BFS,对于每一层取队列中对首元素,然后放入队列的时候,如果有对应左右孩子的话,优先放右孩子,再放左孩子。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function (root) {
    if (!root) return [];
    let queue = [root];
    let res = [];
    while (queue.length) {
        let size = queue.length;
        res.push(queue[0].val);
        while (size--) {
            let node = queue.shift();
            // 优先放右孩子
            node.right && queue.push(node.right);
            node.left && queue.push(node.left);
        }
    }
    return res;
};

最后

文章产出不易,还望各位小伙伴们支持一波!

往期精选:

小狮子前端の笔记仓库

leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)

小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!

访问超逸の博客,方便小伙伴阅读玩耍~

学如逆水行舟,不进则退

Chocolate1999 avatar Sep 25 '20 09:09 Chocolate1999