
- 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 right sibling of a binary tree with parent pointers in C++
In this problem we are given a binary tree and parent pointers. Our task is to Find right sibling of a binary tree with parent pointers.
Let’s take an example to understand the problem,
Input
Node = 3
Output
7
Solution Approach
A simple solution to the problem is finding the leaf node of the nearest ancestor (which is neither the current node nor the parest of the current node) which is at the same level as the current node. This is done by counting the levels while going up and then when coming down counting them down. And then finding the node.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right, *parent; }; Node* newNode(int item, Node* parent) { Node* temp = new Node; temp->data = item; temp->left = temp->right = NULL; temp->parent = parent; return temp; } Node* findRightSiblingNodeBT(Node* node, int level) { if (node == NULL || node->parent == NULL) return NULL; while (node->parent->right == node || (node->parent->right == NULL && node->parent->left == node)) { if (node->parent == NULL || node->parent->parent == NULL) return NULL; node = node->parent; level++; } node = node->parent->right; if (node == NULL) return NULL; while (level > 0) { if (node->left != NULL) node = node->left; else if (node->right != NULL) node = node->right; else break; level--; } if (level == 0) return node; return findRightSiblingNodeBT(node, level); } int main(){ Node* root = newNode(4, NULL); root->left = newNode(2, root); root->right = newNode(5, root); root->left->left = newNode(1, root->left); root->left->left->left = newNode(9, root->left->left); root->left->left->left->left = newNode(3, root->left->left->left); root->right->right = newNode(8, root->right); root->right->right->right = newNode(0, root->right->right); root->right->right->right->right = newNode(7, root->right->right->right); Node * currentNode = root->left->left->left->left; cout<<"The current node is "<<currentNode->data<<endl; Node* rightSibling = findRightSiblingNodeBT(currentNode, 0); if (rightSibling) cout<<"The right sibling of the current node is "<<rightSibling->data; else cout<<"No right siblings found!"; return 0; }
Output
The current node is 3 The right sibling of the current node is 7
- Related Articles
- Program to find out the lowest common ancestor of a binary tree using parent pointers using Python
- Program to find sibling value of a binary tree node in Python
- Left-Child Right-Sibling Representation of Tree
- Binary Search Tree insert with Parent Pointer in C++
- Find Height of Binary Tree represented by Parent array in C++
- Maximum parent children sum in Binary tree in C++
- Find sum of all right leaves in a given Binary Tree in C++
- Program to find sum of the right leaves of a binary tree in C++
- Print Right View of a Binary Tree in C language
- Find maximum among all right nodes in Binary Tree in C++
- Binary Tree Right Side View in C++
- Program to find out the node in the right in a binary tree using Python
- Find Minimum Depth of a Binary Tree in C++
- Find Leaves of Binary Tree in C++
- Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++

Advertisements