Good evening! Here's our prompt for today.
Suppose we're given a binary tree, one with several leaves like such:

We're asked to get all the current leaf values, then delete the leaves. Then we repeat-- we again get the leaf values, and then delete-- proceeding until we're done with the root node.

How could we implement this in an efficient manner?
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
78
'PASSED: ' + 'assert.equal(getLeaves(tree5), [[5, 7, 10], [6, 9], [8]]);'
var assert = require('assert');
​
function getLeaves(root) {
// Fill this in
return root;
}
​
function Node(val) {
this.val = val;
this.left = null;
this.right = null;
}
​
// Regular binary trees
var tree1 = new Node(4);
tree1.left = new Node(1);
tree1.right = new Node(3);
​
var tree2 = new Node(5);
tree2.left = new Node(10);
tree2.left.left = new Node(17);
tree2.left.right = new Node(3);
tree2.right = new Node(8);
​
// Binary search trees
var tree3 = new Node(6);
tree3.left = new Node(3);
​
var tree4 = new Node(5);
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
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.