Sum of all the Boundary Nodes of a Binary Tree
Given a binary tree, the task is to print the sum of all the boundary nodes of the tree.

Examples:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
Output: 28
Input:
1
/ \
2 3
\ /
4 5
\
6
/ \
7 8
Output: 36
Approach: We have already discussed the Boundary Traversal of a Binary tree. Here we will find the sum of the boundary nodes of the given binary tree in four steps:
- Sum up all the nodes of the left boundary,
- Sum up all the leaf nodes of the left sub-tree,
- Sum up all the leaf nodes of the right sub-tree and
- Sum up all the nodes of the right boundary.
We will have to take care of one thing that nodes don’t add up again, i.e. the left most node is also the leaf node of the tree.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // A binary tree node has data, // pointer to left child // and a pointer to right child struct Node { int data; struct Node* left; struct Node* right; }; // Utility function to create a node Node* newNode(int data) { Node* temp = new Node; temp->left = NULL; temp->right = NULL; temp->data = data; return temp; } // Function to sum up all the left boundary nodes // except the leaf nodes void LeftBoundary(Node* root, int& sum_of_boundary_nodes) { if (root) { if (root->left) { sum_of_boundary_nodes += root->data; LeftBoundary(root->left, sum_of_boundary_nodes); } else if (root->right) { sum_of_boundary_nodes += root->data; LeftBoundary(root->right, sum_of_boundary_nodes); } } } // Function to sum up all the right boundary nodes // except the leaf nodes void RightBoundary(Node* root, int& sum_of_boundary_nodes) { if (root) { if (root->right) { RightBoundary(root->right, sum_of_boundary_nodes); sum_of_boundary_nodes += root->data; } else if (root->left) { RightBoundary(root->left, sum_of_boundary_nodes); sum_of_boundary_nodes += root->data; } } } // Function to sum up all the leaf nodes // of a binary tree void Leaves(Node* root, int& sum_of_boundary_nodes) { if (root) { Leaves(root->left, sum_of_boundary_nodes); // Sum it up if it is a leaf node if (!(root->left) && !(root->right)) sum_of_boundary_nodes += root->data; Leaves(root->right, sum_of_boundary_nodes); } } // Function to return the sum of all the // boundary nodes of the given binary tree int sumOfBoundaryNodes(struct Node* root) { if (root) { // Root node is also a boundary node int sum_of_boundary_nodes = root->data; // Sum up all the left nodes // in TOP DOWN manner LeftBoundary(root->left, sum_of_boundary_nodes); // Sum up all the // leaf nodes Leaves(root->left, sum_of_boundary_nodes); Leaves(root->right, sum_of_boundary_nodes); // Sum up all the right nodes // in BOTTOM UP manner RightBoundary(root->right, sum_of_boundary_nodes); // Return the sum of // all the boundary nodes return sum_of_boundary_nodes; } return 0; } // Driver code int main() { Node* root = newNode(10); root->left = newNode(2); root->right = newNode(5); root->left->left = newNode(8); root->left->right = newNode(14); root->right->left = newNode(11); root->right->right = newNode(3); root->left->right->left = newNode(12); root->right->left->right = newNode(1); root->right->left->left = newNode(7); cout << sumOfBoundaryNodes(root); return 0; } |
Java
// Java implementation of the approach class GFG { static int sum_of_boundary_nodes=0; // A binary tree node has data, // pointer to left child static class Node { int data; Node left; Node right; }; // Utility function to create a node static Node newNode(int data) { Node temp = new Node(); temp.left = null; temp.right = null; temp.data = data; return temp; } // Function to sum up all the left boundary nodes // except the leaf nodes static void LeftBoundary(Node root) { if (root != null) { if (root.left != null) { sum_of_boundary_nodes += root.data; LeftBoundary(root.left); } else if (root.right != null) { sum_of_boundary_nodes += root.data; LeftBoundary(root.right); } } } // Function to sum up all the right boundary nodes // except the leaf nodes static void RightBoundary(Node root) { if (root != null) { if (root.right != null) { RightBoundary(root.right); sum_of_boundary_nodes += root.data; } else if (root.left != null) { RightBoundary(root.left); sum_of_boundary_nodes += root.data; } } } // Function to sum up all the leaf nodes // of a binary tree static void Leaves(Node root) { if (root != null) { Leaves(root.left); // Sum it up if it is a leaf node if ((root.left == null) && (root.right == null)) sum_of_boundary_nodes += root.data; Leaves(root.right); } } // Function to return the sum of all the // boundary nodes of the given binary tree static int sumOfBoundaryNodes( Node root) { if (root != null) { // Root node is also a boundary node sum_of_boundary_nodes = root.data; // Sum up all the left nodes // in TOP DOWN manner LeftBoundary(root.left); // Sum up all the // leaf nodes Leaves(root.left); Leaves(root.right); // Sum up all the right nodes // in BOTTOM UP manner RightBoundary(root.right); // Return the sum of // all the boundary nodes return sum_of_boundary_nodes; } return 0; } // Driver code public static void main(String args[]) { Node root = newNode(10); root.left = newNode(2); root.right = newNode(5); root.left.left = newNode(8); root.left.right = newNode(14); root.right.left = newNode(11); root.right.right = newNode(3); root.left.right.left = newNode(12); root.right.left.right = newNode(1); root.right.left.left = newNode(7); System.out.println(sumOfBoundaryNodes(root)); } } // This code is contributed by andrew1234 |
48
Time Complexity: O(N) where N is the number of nodes in the binary tree.
Recommended Posts:
- Boundary Traversal of binary tree
- Iterative Boundary traversal of Complete Binary tree
- Sum of all nodes in a binary tree
- Sum of nodes in top view of binary tree
- XOR of path between any two nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Product of all nodes in a Binary Tree
- Sink Odd nodes in Binary Tree
- Product of all leaf nodes of binary tree
- Print all nodes between two given levels in Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Print nodes in top view of Binary Tree | Set 2
- Print nodes in the Top View of Binary Tree | Set 3
- Count Non-Leaf nodes in a Binary Tree
- Print all even nodes of Binary Search Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : andrew1234



