Node Depths
Problem
Add all the node depths in a binary tree and return that value.
i.e. Return the sum of all nodes' depths.
Code
Python
- Solution 1
- Solution 2
JavaScript
- Solution 1
- Solution 2
TypeScript
- Solution 1
- Solution 2
Java
- Solution 1
- Solution 2
C++
- Solution 1
- Solution 2
Space-Time Complexity
When the tree is balanced:
Time | Space | |
---|---|---|
Average | O(n) | O(h) |
Where n is the number of nodes in the Binary Tree and h is the height of the Binary Tree
Notes
keep in mind
We need to compute the depth of each particular node
The root node is the only node in the tree, that without any other information about the tree, we know what the node's depth is
When we're at the root node, not only do we know what our depth is but we also know what the depth of our two children nodes are
At every level, each node will have been told by its parent what its depth is and then, it can tell each children nodes what their depth is by basically telling them your depth is my depth, plus one
Iterative Approach
solution 1
We're going to be using a stack to traverse the tree
Grab the root node and store it on top of the stack and for every node in the stack, the node's depth
As long as the stack is not empty, apply our algorithm
This means pop the last node from the stack, add depth to running sum and push the children nodes, and their depths, to the top of the stack
Recursive Approach
solution 2
f(n, d) = d + f(l, d+1) + f(r, d+1)
n is the node that we're at
d is the depth of the node
l is left child node
r is right child node