Print Boundary Sum of a Binary Tree
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
First of all we need to understand what the boundary sum of a binary tree is? It's simply the cumulative sum of all boundary nodes surrounding the tree. For the following example:
The boundary nodes are: 2, 7, 2, 5, 11, 4, 9, and 5 (from left to right direction)
So there are four types of node considered to be boundary node:
Thus, the boundary sum is: 45
Algorithm:
For this portion we do a level-order traversal & keep checking whether the traversed node has both its left & right point NULL or not.
If the traversed node has both its pointer NULL then it’s a leaf node & of course add to sum cumulatively.
Set temp to root->left
Set temp to root->right
C++ program to print Boundary Sum of a Binary Tree
Output
need an explanation for this answer? contact us directly to get an explanation for this answer