
- 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
Print Common Nodes in Two Binary Search Trees in C++
In this problem, we are given two binary search trees and we have to find the nodes that are common.
Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.
Example,
Here, we have two binary trees and we have to print all the nodes that are the same for both trees.
Let’s create a program that uses an auxiliary stack to find the solution to this problem. It works by popping elements when two same values arise.
Example
#include<iostream> #include<stack> using namespace std; struct Node{ int key; struct Node *left, *right; }; Node *newNode(int ele){ Node *temp = new Node; temp->key = ele; temp->left = temp->right = NULL; return temp; } void printCommon(Node *tree1, Node *tree2){ stack<Node *> stack1, s1, s2; while (1){ if (tree1){ s1.push(tree1); tree1 = tree1->left; } else if (tree2){ s2.push(tree2); tree2 = tree2->left; } else if (!s1.empty() && !s2.empty()){ tree1 = s1.top(); tree2 = s2.top(); if (tree1->key == tree2->key){ cout << tree1->key << " "; s1.pop(); s2.pop(); tree1 = tree1->right; tree2 = tree2->right; } else if (tree1->key < tree2->key){ s1.pop(); tree1 = tree1->right; tree2 = NULL; } else if (tree1->key > tree2->key){ s2.pop(); tree2 = tree2->right; tree1 = NULL; } } else break; } } void inorderTraversal(struct Node *root){ if (root){ inorderTraversal(root->left); cout<<root->key<<" "; inorderTraversal(root->right); } } struct Node* insertNode(struct Node* node, int key){ if (node == NULL) return newNode(key); if (key < node->key) node->left = insertNode(node->left, key); else if (key > node->key) node->right = insertNode(node->right, key); return node; } int main(){ Node *tree1 = NULL; tree1=insertNode(tree1, 45); tree1=insertNode(tree1, 87); tree1=insertNode(tree1, 12); tree1=insertNode(tree1, 54); tree1=insertNode(tree1, 89); tree1=insertNode(tree1, 19); tree1=insertNode(tree1, 72); cout<<"Binary Tree 1 : "; inorderTraversal(tree1); cout<<endl; Node *tree2=NULL; tree2=insertNode(tree2, 72); tree2=insertNode(tree2, 23); tree2=insertNode(tree2, 13); tree2=insertNode(tree2, 1); tree2=insertNode(tree2, 19); cout<<"Binary Tree 2 : "; inorderTraversal(tree2); cout<<endl; cout<<"Common Nodes between the two trees : "; printCommon(tree1, tree2); return 0; }
Output
Binary Tree 1 : 12 19 45 54 72 87 89 Binary Tree 2 : 1 13 19 23 72 Common Nodes between the two trees : 19 72
- Related Articles
- All Elements in Two Binary Search Trees in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Unique Binary Search Trees in C++
- Unique Binary Search Trees II in C++
- Print all nodes between two given levels in Binary Tree in C++
- Binary Search Trees in Data Structures
- Merge Two Binary Trees in C++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Multidimensional Binary Search Trees
- Optimal Binary Search Trees in Data Structures
- Balanced binary search trees in Data Structure
- Count the Number of Binary Search Trees present in a Binary Tree in C++
- Print all full nodes in a Binary Tree in C++
- Print common nodes on path from root (or common ancestors) in C++

Advertisements