
- 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 cousins of a given node in Binary Treein C++
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,
In this problem, we are given a binary tree and we have a node of the tree and we have to find the cousin nodes of the node. No sibling nodes are to be printed for the binary tree.
Let’s take an example,
For the above binary tree, the cousin node is 5.
To make the concept more clear let’s describe cousin node. In a binary tree, two nodes are said to be cousin nodes if they are at the same level (depth) in the binary tree but don't have the same parent node.
Now, let’s create a solution to this problem.
Here we have to print all the nodes at the same level of the node i.e. all the nodes that are at the same distance from the root node. But we have to eliminate the node that has the same parent as the node itself. For this, we will first find the level of the node and then print all the nodes except the node itself and the node with the same parent.
Example
Now, let's create a program based on this logic −
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node *left, *right; }; Node *newNode(int item){ Node *temp = new Node; temp->data = item; temp->left = temp->right = NULL; return temp; } int levelOfNode(Node *root, Node *node, int level){ if (root == NULL) return 0; if (root == node) return level; int downlevel = levelOfNode(root->left, node, level + 1); if (downlevel != 0) return downlevel; return levelOfNode(root->right, node, level + 1); } void printCousin(Node* root, Node *node, int level){ if (root == NULL || level < 2) return; if (level == 2){ if (root->left == node || root->right == node) return; if (root->left) cout << root->left->data << " "; if (root->right) cout << root->right->data; } else if (level > 2){ printCousin(root->left, node, level - 1); printCousin(root->right, node, level - 1); } } void cousinNode(Node *root, Node *node){ int level = levelOfNode(root, node, 1); printCousin(root, node, level); } int main(){ Node *root = newNode(11); root->left = newNode(15); root->right = newNode(4); root->left->left = newNode(3); root->left->right = newNode(7); root->left->right->right = newNode(9); root->right->left = newNode(17); root->right->right = newNode(8); root->right->left->right = newNode(5); cout<<”The cousin nodes are : \t” cousinNode(root, root->right->right); return 0; }
Output
The cousin nodes are : 3 7
- Related Articles
- Print Ancestors of a given node in Binary Tree in C++
- Print ancestors of a given binary tree node without recursion in C++
- Cousins in Binary Tree in C++
- Program to print path from root to a given node in a binary tree using C++
- Find mirror of a given node in Binary tree in C++
- Print the number of set bits in each node of a Binary Tree in C++ Programming.
- Print all nodes at distance k from a given node in C++
- Find distance from root to given node in a binary tree in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Preorder Successor of a Node in Binary Tree in C++
- Preorder predecessor of a Node in Binary Tree in C++
- Postorder successor of a Node in Binary Tree in C++
- Second Minimum Node In a Binary Tree in C++
- Print all nodes between two given levels in Binary Tree in C++
- Program to print nodes between two given level numbers of a binary tree using C++
