Node Depth
Unravel the intricacies of calculating node depth in data structures with this in-depth tutorial.
Published At
7/3/2021
Reading Time
~ 4 min read
The distance between a node in a Binary Tree and the tree's root is called the node's depth.
Write a function that takes in a Binary Tree and returns the sum of its nodes' depths.
Each BinaryTree node has an integer value, a left child node, and a right child node. Children nodes can either be BinaryTree nodes themselves or None / null.
Sample Input
tree = 1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
tree = 1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
Sample Output
16
// The depth of the node with value 2 is 1.
// The depth of the node with value 3 is 1.
// The depth of the node with value 4 is 2.
// The depth of the node with value 5 is 2.
// Etc..
// Summing all of these depths yields 16.
16
// The depth of the node with value 2 is 1.
// The depth of the node with value 3 is 1.
// The depth of the node with value 4 is 2.
// The depth of the node with value 5 is 2.
// Etc..
// Summing all of these depths yields 16.
Hints
Hint 1
As obvious as it may seem, to solve this question, you'll have to figure out how to compute the depth of any given node; once you know how to do that, you can compute all of the depths and add them up to obtain the desired output.
Hint 2
To compute the depth of a given node, you need information about its position in the tree. Can you pass this information down from the node's parent?
Hint 3
The depth of any node in the tree is equal to the depth of its parent node plus 1. By starting at the root node whose depth is 0, you can pass down to every node in the tree its respective depth, and you can implement the algorithm that does this and that sums up all of the depths either recursively or iteratively.
Optimal Space & Time Complexity
Average case: when the tree is balanced O(n) time | O(h) space - where n is the number of nodes in the Binary Tree and h is the height of the Binary Tree
function nodeDepths(root) {
let sums = [];
findNodeSums(root, 0, sums);
let sum = sums.reduce((a,b) => a+b, 0);
return sum
}
function findNodeSums(node, runningSum, sums) {
if(!node) return;
sums.push(runningSum);
let newRunningSum = runningSum + 1;
if(!node.left && !node.right) {
return;
}
findNodeSums(node.left, newRunningSum, sums);
findNodeSums(node.right, newRunningSum, sums);
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root) {
let sums = [];
findNodeSums(root, 0, sums);
let sum = sums.reduce((a,b) => a+b, 0);
return sum
}
function findNodeSums(node, runningSum, sums) {
if(!node) return;
sums.push(runningSum);
let newRunningSum = runningSum + 1;
if(!node.left && !node.right) {
return;
}
findNodeSums(node.left, newRunningSum, sums);
findNodeSums(node.right, newRunningSum, sums);
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root) {
let sumOfDepth = 0;
const stack = [{node: root, depth: 0}]
while(stack.length > 0) {
let {node, depth} = stack.pop();
if (node == null) continue;
sumOfDepth += depth;
stack.push({node: node.left, depth: depth + 1});
stack.push({node: node.right, depth: depth + 1});
}
return sumOfDepth
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root) {
let sumOfDepth = 0;
const stack = [{node: root, depth: 0}]
while(stack.length > 0) {
let {node, depth} = stack.pop();
if (node == null) continue;
sumOfDepth += depth;
stack.push({node: node.left, depth: depth + 1});
stack.push({node: node.right, depth: depth + 1});
}
return sumOfDepth
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root, depth = 0) {
if (root === null) return 0;
return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1);
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function nodeDepths(root, depth = 0) {
if (root === null) return 0;
return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1);
}
// This is the class of the input binary tree.
class BinaryTree {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
🕳
Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.
Have a wonderful day.
Abhishek 🙏