Good evening! Here's our prompt for today.
A binary search tree
is a data structure that has the following properties,
- Each node in the tree has only two children.
- The left subtree consists of nodes with values less than the root node.
- The right subtree consists of nodes with values greater than the root node.
Consider a binary tree with the root node given as root
. Your task is to return the values of the nodes that are on the right side of the binary tree, ordered from top to bottom.

For example, consider the tree in the image above. On the right side of the tree, we see the values 9, 11, and 16. These are returned as an array [9, 11, 16]
.
Constraints
- The number of nodes in the tree is in the range [0, 100].
100 <= Node.val <= 100
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
57
var assert = require('assert');
​
function TreeNode(val) {
this.val = val
this.left = null
this.right = null
}
​
function rightNodesBST(root) {
return;
}
​
try {
let root = new TreeNode(10)
root.left = new TreeNode(5)
root.right = new TreeNode(15)
root.left.left = new TreeNode(3)
root.left.right = new TreeNode(7)
root.right.right = new TreeNode(18)
assert.deepEqual(rightNodesBST(root), [10, 15, 18]);
​
console.log('PASSED: ' + "rightNodesBST([10, 5, 15, 3, 7, null, 18])` should return `[10, 15, 18]`");
} catch (err) {
console.log(err);
}
​
try {
let root = new TreeNode(1)
root.left = new TreeNode(2)
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.