199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
function rightSideView(root: TreeNode | null): number[] { const res: number[] = []; if (root === null) return res; const nodeQues: TreeNode[] = [root]; while(nodeQues.length > 0) { const childs = nodeQues.splice(0); const clen = childs.length; res.push(childs[clen - 1].val) for(let i = 0; i < clen; i++) { const node = childs[i]; if (node.left) nodeQues.push(node.left) if (node.right) nodeQues.push(node.right) } } return res };
|