# LeetCode: Binary Tree Right Side View Solution

## Approach

Priority from right to left

Keep track of the current depth to check if node is viewed

## Implementation

`.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * Definition for a binary tree node.3 * function TreeNode(val, left, right) {4 *     this.val = (val===undefined ? 0 : val)5 *     this.left = (left===undefined ? null : left)6 *     this.right = (right===undefined ? null : right)7 * }8 */9/**10 * @param {TreeNode} root11 * @return {number[]}12 */13var rightSideView = function (root) {14  const res = []15  const view = function (node, depth) {16    if (!node) return17    if (depth === res.length) {18      res.push(node.val)19    }20    view(node.right, depth + 1)21    view(node.left, depth + 1)22  }23  view(root, 0)24  return res25}`

leetcode

tree

recursion

## Next Post

LeetCode: Shortest Distance To A Character

Feb 7, 2021

## Previous Post

LeetCode: Climbing Stairs

Feb 6, 2021

Memoized recursion

Search Posts