Sum of All Nodes In Binary Tree In C
Sum of all nodes in Binary Tree
Here, in this page we will write a C program to find the sum of all nodes in the given binary tree.
First, we will traverse through the left sub-tree and calculate the sum of nodes present in the left sub-tree. Similarly, we calculate the sum of nodes present in the right sub-tree and calculate total sum by adding the root’s data.
Sum of All Nodes In Binary Tree
Algorithm :
- Create a function say, calculateSum() that will calculate the sum of nodes present in the binary tree.
- It checks whether the root is null, which means that the tree is empty.
- If the tree is not empty, traverse through left subtree, calculate the sum of nodes and store it in variable say, sumLeft.
- Then, traverse through the right subtree, calculate the sum of nodes and store it in variable say, sumRight.
- Finally, calculate total sum = root->data + sumLeft + sumRight.
Code in C based on above approach
Run
#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *left; struct node *right; }; struct node *root = NULL; struct node *createNode (int data) { struct node *newNode = (struct node *) malloc (sizeof (struct node)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } int calculateSum (struct node *temp) { int sum, sumLeft, sumRight; sum = sumRight = sumLeft = 0; if (root == NULL) { printf ("Tree is empty\n"); return 0; } else { if (temp->left != NULL) sumLeft = calculateSum (temp->left); if (temp->right != NULL) sumRight = calculateSum (temp->right); sum = temp->data + sumLeft + sumRight; return sum; } } int main () { root = createNode (10); root->left = createNode (20); root->right = createNode (30); root->left->left = createNode (40); root->right->left = createNode (50); root->right->right = createNode (60); printf ("Sum of all nodes of binary tree: %d", calculateSum (root)); return 0; }
Output:
Sum of all nodes of binary tree: 210
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Login/Signup to comment