Here is the interview question prompt, presented for reference.
Assume we're using a binary tree
in writing a video game via Binary Space Partitioning. We need to identify the bottom left leaf, that is-- the leftmost value in the lowest row of the binary tree.
In this example, the bottom left leaf
is 3
:
/*
4
/ \
3 5
*/
Assuming the standard node definition of:
function Node(val) {
this.val = val;
this.left = this.right = null;
}
It would be called as such:
const root = new Node(4);
root.left = new Node(3);
root.right = new Node(5);
function bottomLeftNodeVal(root) { return; };
bottomLeftNodeVal(root);
// 3
Here's another example. Let's assume that there's at least the root node available in all binary trees passed as parameters.
/*
4
/ \
1 3
/ \
5 9
*/
const root = new Node(4);
root.left = new Node(1);
root.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(9);
bottomLeftNodeVal(root);
// 5
100000
-1000000000
and 1000000000
O(n)
O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Bottom Left Node Value.