
- 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 if there is a pair in root to a leaf path with sum equals to root's data in C++
In this problem, we are given a Binary Tree. And we need to find if there is a pair in root to a leaf path with sum equals to root’s data.
We need to check if there exists a pair of nodes that lies between root node to leaf nodes such that the sum of values of pairs is equal to the root node’s value.
Let’s take an example to understand the problem,
Input:
Output: Yes
Explanation:
Root node value 7
Pairs with sum value equal to root node, (2, 5), (1, 6).
Solution Approach:
We need to traverse the tree and find the pairs using hashing.
For this we will create a hashTable and traverse tree. Insert data into hashtable and then check if its sum with other elements is equal to root.
And at the end, if we find no pairs, return false.
If pairs are found, return true.
Program to illustrate the working of our solution,
Example
#include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* left, *right; }; struct Node* newnode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } bool findSumUntill(Node *node, unordered_set<int> &hashTable, int rootVal) { if (node == NULL) return false; int otherVal = rootVal - node->data; if (hashTable.find(otherVal) != hashTable.end()) return true; hashTable.insert(node->data); bool isFound = findSumUntill(node->left, hashTable, rootVal) || findSumUntill(node->right, hashTable, rootVal); hashTable.erase(node->data); return isFound; } bool findPairSum(Node *root) { unordered_set<int> hashTable; return findSumUntill(root->left, hashTable, root->data) || findSumUntill(root->right, hashTable, root->data); } int main() { struct Node *root = newnode(7); root->left = newnode(2); root->right = newnode(3); root->left->left = newnode(5); root->left->right = newnode(9); root->left->left->left = newnode(1); root->left->left->right = newnode(6); root->right->left = newnode(8); if(findPairSum(root)) cout<<"Pair with sum equal to root value found"; else cout<<"No pairs found"; return 0; }
Output
Pair with sum equal to root value found
- Related Articles
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Print all root to leaf paths with there relative positions in C++
- Find if there is a subarray with 0 sum in C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Sum Root to Leaf Numbers in Python
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- JavaScript Program to Find if there is a subarray with 0 sum
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Find a pair with given sum in BST in C++
- Find if there is a path of more than k length from a source in C++
- Find a pair with given sum in a Balanced BST in C++
- C++ program to find whether there is a path between two cells in matrix
- Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++
- Count rows/columns with sum equals to diagonal sum in C++
- Find the Pair with a Maximum Sum in a Matrix using C++
