题目:
199. 二叉树的右视图
题解:
思路:递归,树的深度遍历
代码:


var rightSideView = function (root) {
    let res = [];
    let arr = [];

    dfs(root, 1)
    return res;

    function dfs(r, h) {
        //主要在这,递归结束条件
        if (r === null) return;

        if (!arr[h]) {
            arr[h] = r.val
            res.push(r.val)
        }
        r.right && dfs(r.right, h + 1)
        r.left && dfs(r.left, h + 1)
    }

};

相关文章:

  • 2021-08-04
  • 2021-09-23
  • 2022-12-23
  • 2022-02-06
  • 2021-09-11
  • 2022-01-17
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2020-01-05
  • 2021-10-25
  • 2021-11-05
  • 2022-12-23
  • 2021-12-02
  • 2021-10-28
相关资源
相似解决方案