- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find n-th node of inorder traversal in C++
In this problem, we are given a binary tree and an integer N. The task is to find the n-th node in inorder traversal of a Binary Tree.
A binary tree has a special condition that each node can have a maximum of two children.
Traversal is a process to visit all the nodes of a tree and may print their values too.
Let’s take an example to understand the problem,
Input
N = 6
Output
3
Explanation
inorder traversal of tree : 4, 2, 5, 1, 6, 3, 7
Solution Approach
The idea is to use the in-order traversal of the binary tree which is done by using recursive calls. In each call we will call preOrder() for left subtree first and traverse the root node then call preOrder(). During this traversal, we will count the number of nodes and print the node for which the count is N.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; struct Node { int data; Node *left, *right; }; struct Node* createNode(int item){ Node* temp = new Node; temp->data = item; temp->left = NULL; temp->right = NULL; return temp; } void findInOrderTraversalRec(struct Node* node, int N){ static int count = 0; if (node == NULL) return; if (count <= N) { findInOrderTraversalRec(node->left, N); count++; if (count == N) cout<<node->data; findInOrderTraversalRec(node->right, N); } } int main() { struct Node* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->left = createNode(6); root->right->right = createNode(7); int N = 6; cout<<N<<"th node in inorder traversal is "; findInOrderTraversalRec(root, N); return 0; }
Output
6th node in inorder traversal is 3
- Related Articles
- Find n-th node in Postorder traversal of a Binary Tree in C++
- Find n-th node in Preorder traversal of a Binary Tree in C++
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Find the fractional (or n/k – th) node in linked list in C++
- Inorder Traversal of a Threaded Binary Tree in C++
- Binary Tree Inorder Traversal in Python
- C++ Program for Inorder Tree Traversal without Recursion
- Construct Special Binary Tree from given Inorder traversal in C++
- Find the kth node in vertical order traversal of a Binary Tree in C++
- Find the second last node of a linked list in single traversal in C++
- Kth node in Diagonal Traversal of Binary Tree in C++
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- Find n-th lexicographically permutation of a strings in C++
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree

Advertisements