
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a given Binary Tree is SumTree in C++
Here we will see how to check whether a binary tree is sum-tree or not. Now the question is what is the sum-tree. A sum-tree is a binary tree where a node will hold the sum value of its children. The root of the tree will contain an entire sum of all elements below it. This is an example of sum-tree −
To check this, we will follow a simple trick, we will find the sum of left and right subtree elements if the sum value is the same as the root, then that is sum-tree. This will be one recursive approach.
Example
#include <bits/stdc++.h> using namespace std; class node { public: int data; node* left, *right; }; int sum_of_nodes(node *root) { if(root == NULL) return 0; return sum_of_nodes(root->left) + root->data + sum_of_nodes(root->right); } int isSumTree(node* node) { int left_sum, right_sum; if(node == NULL || (node->left == NULL && node->right == NULL)) return 1; left_sum = sum_of_nodes(node->left); right_sum = sum_of_nodes(node->right); if((node->data == left_sum + right_sum) && isSumTree(node->left) && isSumTree(node->right)) return 1; return 0; } node* getNode(int data) { node* newNode = new node(); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } int main() { node *root = getNode(26); root->left = getNode(10); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(6); root->right->right = getNode(3); if(isSumTree(root)) cout << "The tree is Sum Tree"; else cout << "The tree is not a Sum Tree"; }
Output
The tree is Sum Tree
- Related Articles
- Check if a given Binary Tree is Heap in C++
- Check if a given Binary Tree is Heap in Python
- Check if a binary tree is subtree of another binary tree in C++
- Check if a given Binary Tree is height balanced like a Red-Black Tree in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Check if a given Binary Tree is height balanced like a Red-Black Tree in Python
- C++ Program to Check if a Binary Tree is a BST
- Check if a binary tree is sorted levelwise or not in C++
- Check if a given array can represent Preorder Traversal of Binary Search Tree in C++
- Check if a binary tree is sorted level-wise or not in C++
- A program to check if a binary tree is BST or not in C ?
- Check if a given tree graph is linear or not in C++
- How to check whether a binary tree is a valid binary search tree using recursion in C#?

Advertisements