
- 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 n-th node in Preorder traversal of a Binary Tree 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 Preorder 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
6
Explanation
Pre order traversal of tree : 1, 2, 4, 5, 3, 6, 7
Solution Approach
The idea is to use the pre-order traversal of the binary tree which is done by using recursive call. In each call we will visit the root node then call preOrder() for left subtree first and 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 findPreOrderTraversalRec(struct Node* root, int N){ static int nodeCount = 0; if (root == NULL) return; if (nodeCount <= N) { nodeCount++; if (nodeCount == N) cout << root->data; findPreOrderTraversalRec(root->left, N); findPreOrderTraversalRec(root->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 preorder traversal is "; findPreOrderTraversalRec(root, N); return 0; }
Output
6th node in preorder traversal is 6
- Related Articles
- Find n-th node in Postorder traversal of a Binary Tree in C++
- Binary Tree Preorder Traversal in Python
- Find n-th node of inorder traversal in C++
- Preorder Successor of a Node in Binary Tree in C++
- Preorder predecessor of a Node in Binary Tree in C++
- N-ary Tree Preorder Traversal in C++
- Construct Binary Search Tree from Preorder Traversal in Python
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Preorder Traversal of N-ary Tree Without Recursion in C++
- Kth node in Diagonal Traversal of Binary Tree in C++
- Find the kth node in vertical order traversal of a Binary Tree in C++
- Golang Program to traverse a given binary tree in Preorder Traversal (Recursive)
- C++ Program to Perform Preorder Recursive Traversal of a Given Binary Tree
- Preorder Tree Traversal in Data Structures

Advertisements