
- 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 Ancestors of a given node in Binary Tree in C++
In this problem, we are given a binary tree and we have to print its ancestor of a node in a binary tree.
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,
The ancestor of a node in a binary tree is a node that is at the upper level of the given node.
Let’s take an example of ancestor node −
Ancestors of a node with value 3 in this binary tree are 8,
For solving this problem, we will traverse from the root node to the target node. Step by step downwards in the binary tree. And in the path print all the nodes that come.
Example
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node{ int data; struct node* left; struct node* right; }; bool AncestorsNodes(struct node *root, int target){ if (root == NULL) return false; if (root->data == target) return true; if ( AncestorsNodes(root->left, target) || AncestorsNodes(root->right, target) ){ cout << root->data << " "; return true; } return false; } struct node* insertNode(int data){ struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } int main(){ struct node *root = insertNode(10); root->left = insertNode(6); root->right = insertNode(13); root->left->left = insertNode(3); root->left->right = insertNode(8); root->right->left = insertNode(12); cout<<"Ancestor Nodes are " ; AncestorsNodes(root, 8); getchar(); return 0; }
Output
Ancestor Nodes are 6 10
- Related Articles
- Print ancestors of a given binary tree node without recursion in C++
- Find mirror of a given node in Binary tree in C++
- Print cousins of a given node in Binary Treein C++
- Program to print path from root to a given node in a binary tree using C++
- Print the number of set bits in each node of a Binary Tree in C++ Programming.
- Find distance from root to given node in a binary tree in C++
- 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++
- Print Binary Tree in C++
- Second Minimum Node In a Binary Tree in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.
- Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++
- Deepest left leaf node in a binary tree in C++
- Find the Deepest Node in a Binary Tree in C++

Advertisements