Example: 1 / \ 2 3 / \ / \ 4 5 6 7 / 8 ==> sum of all nodes = 36 (1+2+3+4+5+6+7+8)
Queue
for levelorder traversal.
1. Declare a Queue
to store the nodes
of the tree.
2. Queue the root
.
3. Loop while the queue is not empty.
4. Within the loop, dequeue a node and add its data to the overall sum. Queue the left and right children.
5. Return the sum
outside the loop.
public int sumItr(TreeNode root) { }
C
Java
Python