Loading problem...
You are given the root of a binary tree. Your task is to compute and return the total sum of all node values located at the bottommost level of the tree.
The bottommost level (also called the deepest level) refers to the level that is farthest from the root node. A level is defined as a set of nodes that are at the same distance from the root. The root node is at level 0, its children are at level 1, and so on.
In other words, you need to identify all the leaf-like nodes that exist at the maximum depth of the tree and calculate the sum of their values. Note that the deepest nodes are not necessarily traditional leaves—they are simply the nodes at the greatest depth, which may or may not have children (though by definition, the deepest nodes in a tree will always be leaves since there are no nodes below them).
Key Concepts:
Your algorithm should efficiently traverse the tree to find the maximum depth and then aggregate the values of all nodes residing at that depth.
root = [1,2,3,4,5,null,6,7,null,null,null,null,8]15The binary tree has 4 levels (depths 0 to 3). The nodes at the deepest level (depth 3) are: 7 and 8. Their sum is 7 + 8 = 15.
root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]19The deepest level (depth 3) contains nodes with values: 9, 1, 4, and 5. Their sum is 9 + 1 + 4 + 5 = 19.
root = [1,2,3,4,5,6,7]22This is a complete binary tree with 3 levels. The deepest level (depth 2) contains nodes: 4, 5, 6, and 7. Their sum is 4 + 5 + 6 + 7 = 22.
Constraints