AfterAcademy Tech
•
05 Nov 2020

Difficulty: Easy
Problem Description
Given a binary tree, write a program to return the average value of the nodes on each level in the form of an array.
Problem Note
Example
Input:
4
/ \
8 11
/ \
13 7
Output: [4, 9.5, 10]
Explanation: The average value of nodes on level 0 is 4, on level 1 is 9.5, and on level 2 is 10. Hence return [4, 9.5, 10].
The node structure passed to the function is:
struct TreeNode{
int val
TreeNode left
TreeNode right
}
You can try practising this problem here.
You must have read about the Level Order Traversal in a tree that is used to traverse in a binary tree. The level order traversal is similar to BFS traversal where the starting point is the root node. We can use this traversal technique here.
How can we use Level Order Traversal Technique to solve the problem?
We will use a Queue that will store the nodes of each level. We will enqueue all the nodes of the current level while storing their sum in a variable. The total number of nodes in the current level will be equal to the queue size. Using them we can find the average of the current level and store it in the result array. Afterwards, we will dequeue all the nodes from Queue and enqueue the children of the dequeued nodes which will represent the next level of the tree.
Solution Step
currLevelNoOfNodes variablesum variable that will store the sum of all the nodes of the current level.(sum/currLevelNoOfNodes) in the result array.3. Repeat step 2 until the Queue becomes empty.
Pseudo Code
float[] averageOfLevels(TreeNode root){
float[] result
Queue q
q.push(root)
result.append(root.val)
while(q is not empty){
sum = 0
prevLevelNoOfNodes = q.size()
currLevelNoOfNodes = 0
while(prevLevelNoOfNodes > 0){
TreeNode node = q.pop()
prevLevelNoOfNodes = prevLevelNoOfNodes - 1
if(node.left){
q.push(node.left)
sum = sum + node.left.val
currLevelNoOfNodes = currLevelNoOfNodes + 1
}
if(node.right){
q.push(node.right)
sum = sum + node.right.val
currLevelNoOfNodes = currLevelNoOfNodes + 1
}
}
if(currLevelNoOfNodes > 0){
result.append(sum/currLevelNoOfNodes)
}
}
return result
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Critical Ideas To Think
currLevelNoOfNodes > 0 ? Answer: This is because if the currLevelNoOfNodes is 0, that means there were no nodes at that level and thereby it will not contribute to the result array as all the levels of the tree been traversed by then.prevLevelNoOfNodes are equal to the queue size?Please comment down below if you have a better insight in the above approach.
Happy Coding, Enjoy Algorithms!
AfterAcademy Tech
Given a binary tree, return the zigzag level order traversal of its nodes' values. (i.e, from left to right, then right to left for the next level and alternate between). The problem is a typical Interview problem based on Tree Traversal

AfterAcademy Tech
Given a binary tree, invert the binary tree and return it. An inverted form of a Binary Tree is another Binary Tree with left and right children of all non-leaf nodes interchanged. You may also call it the mirror of the input tree.

AfterAcademy Tech
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question
