Find n-th node of inorder traversal
Given the binary tree and you have to find out the n-th node of inorder traversal.
Examples:
Input : n = 4
10
/ \
20 30
/ \
40 50
Output : 10
Inorder Traversal is : 40 20 50 10 30
Input : n = 3
7
/ \
2 3
/ \
8 5
Output : 8
Inorder: 2 7 8 3 5
3th node is 8
We do simple Inorder Traversal. While doing the traversal, we keep track of the count of nodes visited so far. When the count becomes n, we print the node.
Algorithm:
Step 1: Start
Step 2: Initialize a static integer variable count to 0.
Step 3: Define a function name it as newNode which take integer data as input and set initially set left = right = null.
Step 4: Create a static function of void return type name it as NthInorder which takes reference to node and integer value as input parameterwhich print nth node of inorder.
a. set base condition that is if node equal to null then return
b. Check if the count is less than or equal to n. If the count is less than or equal to n, then do the following:
1. Call NthInorder(node->left, n) to repeat on the left child.
2. Add one to the count.
3. Verify that the count equals n, and if it does, call cout node->data endl; to print the data of the current node.
4. Call NthInorder(node->right, n) to repeat the action on the right child.
Step 5: End
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* node
= ( struct Node*) malloc ( sizeof ( struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void NthInorder( struct Node* node, int n)
{
static int count = 0;
if (node == NULL)
return ;
if (count <= n) {
NthInorder(node->left, n);
count++;
if (count == n)
cout << node->data<< endl;
NthInorder(node->right, n);
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
int n = 4;
NthInorder(root, n);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* node =
( struct Node*) malloc ( sizeof ( struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void NthInorder( struct Node* node, int n)
{
static int count = 0;
if (node == NULL)
return ;
if (count <= n) {
NthInorder(node->left, n);
count++;
if (count == n)
printf ( "%d " , node->data);
NthInorder(node->right, n);
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
int n = 4;
NthInorder(root, n);
return 0;
}
|
Java
import java.io.*;
import java.util. *;
class Solution
{
static int count = 0 ;
static class Node {
int data;
Node left;
Node right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static void NthInorder( Node node, int n)
{
if (node == null )
return ;
if (count <= n) {
NthInorder(node.left, n);
count++;
if (count == n)
System.out.printf( "%d " , node.data);
NthInorder(node.right, n);
}
}
public static void main(String args[])
{
Node root = newNode( 10 );
root.left = newNode( 20 );
root.right = newNode( 30 );
root.left.left = newNode( 40 );
root.left.right = newNode( 50 );
int n = 4 ;
NthInorder(root, n);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
self .visited = False
count = [ 0 ]
def NthInorder(node, n):
if (node = = None ):
return
if (count[ 0 ] < = n):
NthInorder(node.left, n)
count[ 0 ] + = 1
if (count[ 0 ] = = n):
print (node.data, end = " " )
NthInorder(node.right, n)
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 20 )
root.right = newNode( 30 )
root.left.left = newNode( 40 )
root.left.right = newNode( 50 )
n = 4
NthInorder(root, n)
|
C#
using System;
class GFG
{
public static int count = 0;
public class Node
{
public int data;
public Node left;
public Node right;
}
public static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void NthInorder(Node node, int n)
{
if (node == null )
{
return ;
}
if (count <= n)
{
NthInorder(node.left, n);
count++;
if (count == n)
{
Console.Write( "{0:D} " , node.data);
}
NthInorder(node.right, n);
}
}
public static void Main( string [] args)
{
Node root = newNode(10);
root.left = newNode(20);
root.right = newNode(30);
root.left.left = newNode(40);
root.left.right = newNode(50);
int n = 4;
NthInorder(root, n);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data=data;
this .left= this .right= null ;
}
}
let count =0;
function NthInorder(node,n)
{
if (node == null )
return ;
if (count <= n) {
NthInorder(node.left, n);
count++;
if (count == n)
document.write(node.data+ " " );
NthInorder(node.right, n);
}
}
let root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
let n = 4;
NthInorder(root, n);
</script>
|
Time Complexity: O(N), where N is the number of nodes in the given binary tree. We need to traverse the given binary tree to find the nth node of inorder traversal.
Auxiliary Space: O(h), where h is the height of the binary tree. The maximum number of function calls on the call stack at any given point is equal to the height of the tree.
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...