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)
}
};