
- 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
Find Count of Single Valued Subtrees in C++
Suppose we have a binary tree. Our task is to count single valued subtrees in the given tree. A single valued subtree is a subtree, where all nodes of that tree is containing same value. Suppose a tree is like below −
There are four Single value subtrees. These are like below −
We can solve this using bottom up manner. For every subtree visited, return true, if the subtree, rooted under it is single valued, and increase the counter by 1. Here count is the reference parameter for recursive call. And use the returned value to find out if left and right subtrees are single valued or not.
Example
#include <iostream> using namespace std; class Node { public: int data; Node* left, *right; }; Node* getNode(int data) { Node* newNode = new Node; newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } bool countSingleValuedSubtree(Node* root, int &count) { if (root == NULL) return true; bool left = countSingleValuedSubtree(root->left, count); bool right = countSingleValuedSubtree(root->right, count); if (left == false || right == false) return false; if (root->left && root->data != root->left->data) return false; if (root->right && root->data != root->right->data) return false; count++; return true; } int countSingleValSubtree(Node* root) { int count = 0; countSingleValuedSubtree(root, count); return count; } int main() { Node* root = getNode(5); root->left = getNode(1); root->right = getNode(5); root->left->left = getNode(5); root->left->right = getNode(5); root->right->right = getNode(5); cout << "Count of Single Valued Subtrees is: " << countSingleValSubtree(root); }
Output
Count of Single Valued Subtrees is: 4
- Related Articles
- Count Univalue Subtrees in C++
- Find Duplicate Subtrees in C++
- Count BST subtrees that lie in given range in C++
- Find All Duplicate Subtrees in C++
- Count subtrees that sum up to a given value x in C++
- Find largest subtree having identical left and right subtrees in Python
- Implement multiple COUNT() in a single MySQL query
- Sum of Nodes with Even-Valued Grandparent in C++
- Get the count of two table fields in a single MySQL query?
- Get the count of duplicate values from a single column in MySQL?
- Program to find maximum profit by selling diminishing-valued colored balls in Python
- Count two different columns in a single query in MySQL?
- Python – Filter unique valued tuples
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Program to find the largest and smallest ASCII valued characters in a string in C++

Advertisements