Node Depth

A corgi smiling happily

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

1tree = 1
2 / \
3 2 3
4 / \ / \
5 4 5 6 7
6 / \
78 9

Sample Output

116
2// The depth of the node with value 2 is 1.
3// The depth of the node with value 3 is 1.
4// The depth of the node with value 4 is 2.
5// The depth of the node with value 5 is 2.
6// Etc..
7// 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

Solution-1
1function nodeDepths(root) {
2 let sums = [];
3 findNodeSums(root, 0, sums);
4
5 let sum = sums.reduce((a,b) => a+b, 0);
6 return sum
7}
8
9function findNodeSums(node, runningSum, sums) {
10 if(!node) return;
11
12 sums.push(runningSum);
13
14 let newRunningSum = runningSum + 1;
15 if(!node.left && !node.right) {
16 return;
17 }
18
19 findNodeSums(node.left, newRunningSum, sums);
20 findNodeSums(node.right, newRunningSum, sums);
21}
22
23// This is the class of the input binary tree.
24class BinaryTree {
25 constructor(value) {
26 this.value = value;
27 this.left = null;
28 this.right = null;
29 }
30}
Solution-2
1function nodeDepths(root) {
2 let sumOfDepth = 0;
3 const stack = [{node: root, depth: 0}]
4 while(stack.length > 0) {
5 let {node, depth} = stack.pop();
6 if (node == null) continue;
7 sumOfDepth += depth;
8 stack.push({node: node.left, depth: depth + 1});
9 stack.push({node: node.right, depth: depth + 1});
10 }
11 return sumOfDepth
12}
13
14// This is the class of the input binary tree.
15class BinaryTree {
16 constructor(value) {
17 this.value = value;
18 this.left = null;
19 this.right = null;
20 }
21}
Solution-3
1function nodeDepths(root, depth = 0) {
2 if (root === null) return 0;
3 return depth + nodeDepths(root.left, depth + 1) + nodeDepths(root.right, depth + 1);
4}
5
6// This is the class of the input binary tree.
7class BinaryTree {
8 constructor(value) {
9 this.value = value;
10 this.left = null;
11 this.right = null;
12 }
13}

🕳

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 🙏

Subscribe to my newsletter

Get email from me about my ideas, full-stack development resources, tricks and tips as well as exclusive previews of upcoming articles.

No spam. Just the highest quality ideas you’ll find on the web.