I am new to data structures in JavaScript and am trying to learn Binary Search Trees. I was following along with a blog post and was able to get a working solution to the problem of finding the max depth in a BST, but it's unclear to me how the recursion is working and how the +1 gets added on each time at each level of depth. What is a good way to think about this? Is it basically that each time the nodes value is not null, 1 gets added to what will eventually be returned up the call stack (i.e. at each level as it backtracks up to the root)?
function maxDepth(node) {
// console.log(node.left);
if (node) {
return Math.max(maxDepth(node.left), maxDepth(node.right)) + 1;
} else {
return 0;
}
}
I am new to data structures in JavaScript and am trying to learn Binary Search Trees. I was following along with a blog post and was able to get a working solution to the problem of finding the max depth in a BST, but it's unclear to me how the recursion is working and how the +1 gets added on each time at each level of depth. What is a good way to think about this? Is it basically that each time the nodes value is not null, 1 gets added to what will eventually be returned up the call stack (i.e. at each level as it backtracks up to the root)?
function maxDepth(node) {
// console.log(node.left);
if (node) {
return Math.max(maxDepth(node.left), maxDepth(node.right)) + 1;
} else {
return 0;
}
}
Share
Improve this question
edited Feb 2, 2020 at 1:34
mazhar islam
5,6193 gold badges22 silver badges42 bronze badges
asked Nov 29, 2015 at 0:22
devdropper87devdropper87
4,18713 gold badges48 silver badges72 bronze badges
1
|
2 Answers
Reset to default 16The code for maxDepth(node)
reads like this:
If
node
is notnull
:- Run this same algorithm
maxDepth
onnode
's left child. Let this answer bex
. - Run this same algorithm
maxDepth
onnode
's right child. Let this answer bey
. - Calculate
Math.max(x, y) + 1
, and return this value as the answer for this function call.
- Run this same algorithm
Otherwise
node
isnull
, then return0
.
This means when we try to compute maxDepth(node)
on a non-null node, we first compute maxDepth()
on both of node
's children, and let those two subcomputations finish. Then we take the maximum of these values, add 1, and return the result.
Example:
a
/ \
b f
/ \ \
c e g
/
d
Call stack:
a => max(b,f)
b => max(c,e)
c => max(d,null)
d => max(null,null)
d <= (0,0)+1 = 1
c <= (1,0)+1 = 2
e => max(null,null)
e <= (0,0)+1 = 1
b <= (2,1)+1 = 3
f => (null,g)
g => (null,null)
g <= (0,0)+1 = 1
f <= (0,1)+1 = 2
a <= (3,2)+1 = 4
Let me rewrite the code in a little simpler way for the sake of an easy and better explanation.
function maxDepth(node) {
if (node == null)
return 0;
else {
l = maxDepth(node.left)
r = maxDepth(node.right)
return Math.max(left, right) + 1;
}
}
Now, let's explain the above recursion with the following tree:
A
/ \
B C
/
D
The function maxDepth(node)
get called with the root (A
), therefore, we will explain our recursion stack pictorially starting from node A
:
A
| l = ?
|-------> B
| | l = ?
| |-------> D
| | | l = ?
| | |-------> null (return 0)
A
| l = ?
|-------> B
| | l = ?
| |-------> D
| | | l = 0 <---------|
| | |-------> null (return 0)
A
| l = ?
|-------> B
| | l = ?
| |-------> D
| | | l = 0
| | |
| | | r = ?
| | |-------> null (return 0)
A
| l = ?
|-------> B
| | l = ?
| |-------> D
| | | l = 0
| | |
| | | r = 0 <---------|
| | |-------> null (return 0)
A
| l = ?
|-------> B
| | l = ? <--------------------------|
| |-------> D |
| | | l = 0 |
| | | max(0,0)+1 => 1
| | | r = 0
A
| l = ?
|-------> B
| | l = 1 <--------------------------|
| |-------> D |
| | | l = 0 |
| | | max(0,0)+1 => 1
| | | r = 0
A
| l = ?
|-------> B
| | l = 1
| |
| | r = ?
| | -------> null (return 0)
A
| l = ?
|-------> B
| | l = 1
| |
| | r = 0 <---------|
| | -------> null (return 0)
A
| l = ? <--------------------------|
|-------> B |
| | l = 1 |
| | max(1,0)+1 => 2
| | r = 0
A
| l = 2 <--------------------------|
|-------> B |
| | l = 1 |
| | max(1,0)+1 => 2
| | r = 0
A
| l = 2
|
| r = ?
| -------> C
| | l = ? <---------|
| |-------> null (return 0)
A
| l = 2
|
| r = ?
| -------> C
| | l = 0
| |
| | r = ? <---------|
| |-------> null (return 0)
A
| l = 2
|
| r = ? <---------------------------|
| -------> C |
| | l = 0 |
| | max(0,0)+1 => 1
| | r = 0
A
| l = 2
|
| r = 1 <---------------------------|
| -------> C |
| | l = 0 |
| | max(0,0)+1 => 1
| | r = 0
A <----------------------|
| l = 2 |
| max(2,1)+1 => 3
| r = 1
Finally, A
returns 3
.
3
^
|
A (3)<-------------------|
| l = 2 |
| max(2,1)+1 => 3
| r = 1
maxDepth()
function, is enough scope for us to answer. You can delete the second block of code. – Nayuki Commented Nov 29, 2015 at 0:26