[Naive Approach] Using In-order traversal - O(n) Time and O(h) Space
The idea is to traverse the binary tree in in-order manner and maintain the count of nodes visited so far. For each node, increment the count. If the count becomes equal to n, then return the value of current node. Otherwise, check left and right subtree of node. If nth node is not found in current tree, then return -1.
Below is the implementation of the above approach.
C++
// C++ program for nth node of inorder traversals#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Given a binary tree, print its nth inorder node.intnthInorder(Node*root,int&n){// base caseif(root==nullptr)return-1;intleft=nthInorder(root->left,n);// if nth node is found in left subtreeif(left!=-1)returnleft;// If curr node is the nth node if(n==1)returnroot->data;n--;intright=nthInorder(root->right,n);returnright;}intmain(){// hard coded binary tree.// 10// / \ // 20 30// / \ // 40 50Node*root=newNode(10);root->left=newNode(20);root->right=newNode(30);root->left->left=newNode(40);root->left->right=newNode(50);intn=4;cout<<nthInorder(root,n)<<endl;return0;}
Java
// Java program for nth node of inorder traversalsclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Given a binary tree, print its nth inorder node.staticintnthInorder(Noderoot,int[]n){// base caseif(root==null)return-1;intleft=nthInorder(root.left,n);// if nth node is found in left subtreeif(left!=-1)returnleft;// If curr node is the nth node if(n[0]==1){returnroot.data;}n[0]--;intright=nthInorder(root.right,n);returnright;}publicstaticvoidmain(String[]args){// hard coded binary tree.// 10// / \// 20 30// / \// 40 50Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);int[]n={4};System.out.println(nthInorder(root,n));}}
Python
# Python program for nth node of inorder traversalsclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Given a binary tree, print its nth inorder node.defnthInorder(root,n):# base caseifrootisNone:return-1left=nthInorder(root.left,n)# if nth node is found in left subtreeifleft!=-1:returnleft# If curr node is the nth node ifn[0]==1:returnroot.datan[0]-=1right=nthInorder(root.right,n)returnrightif__name__=="__main__":# hard coded binary tree.# 10# / \# 20 30# / \# 40 50root=Node(10)root.left=Node(20)root.right=Node(30)root.left.left=Node(40)root.left.right=Node(50)n=[4]print(nthInorder(root,n))
C#
// C# program for nth node of // inorder traversalsusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Given a binary tree, print its nth inorder node.staticintnthInorder(Noderoot,refintn){// base caseif(root==null)return-1;intleft=nthInorder(root.left,refn);// if nth node is found in left subtreeif(left!=-1)returnleft;// If curr node is the nth node if(n==1){returnroot.data;}n--;intright=nthInorder(root.right,refn);returnright;}staticvoidMain(string[]args){// hard coded binary tree.// 10// / \// 20 30// / \// 40 50Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);intn=4;Console.WriteLine(nthInorder(root,refn));}}
JavaScript
// JavaScript program for nth node// of inorder traversalsclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Given a binary tree, print // its nth inorder node.functionnthInorder(root,n){// base caseif(root===null)return-1;letleft=nthInorder(root.left,n);// if nth node is found in left subtreeif(left!==-1)returnleft;// If curr node is the nth node if(n[0]===1){returnroot.data;}n[0]--;letright=nthInorder(root.right,n);returnright;}// hard coded binary tree.// 10// / \// 20 30// / \// 40 50letroot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);letn=[4];console.log(nthInorder(root,n));
Output
10
[Expected Approach] Using Morris Traversal Algorithm - O(n) Time and O(1) Space
The idea is to use Morris Traversal Algorithm to perform in-order traversal of the binary tree, while maintaining the count of nodes visited so far.
Below is the implementation of the above approach:
C++
// C++ program for nth node of inorder traversals#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Given a binary tree, print its nth inorder node.intnthInorder(Node*root,intn){Node*curr=root;while(curr!=nullptr){// if left child is null, check // curr node and move to right node.if(curr->left==nullptr){if(n==1)returncurr->data;n--;curr=curr->right;}else{// Find the inorder predecessor of currNode*pre=curr->left;while(pre->right!=nullptr&&pre->right!=curr)pre=pre->right;// Make curr as the right child of its// inorder predecessor and move to // left node.if(pre->right==nullptr){pre->right=curr;curr=curr->left;}// Revert the changes made in the 'if' part to// restore the original tree i.e., fix the right// child of predecessorelse{pre->right=nullptr;if(n==1)returncurr->data;n--;curr=curr->right;}}}// If number of nodes is less than n.return-1;}intmain(){// hard coded binary tree.// 10// / \ // 20 30// / \ // 40 50Node*root=newNode(10);root->left=newNode(20);root->right=newNode(30);root->left->left=newNode(40);root->left->right=newNode(50);intn=4;cout<<nthInorder(root,n)<<endl;return0;}
C
// C program for nth node of inorder traversals#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// Given a binary tree, print its nth inorder node.intnthInorder(structNode*root,intn){structNode*curr=root;while(curr!=NULL){// if left child is null, check // curr node and move to right node.if(curr->left==NULL){if(n==1)returncurr->data;n--;curr=curr->right;}else{// Find the inorder predecessor of currstructNode*pre=curr->left;while(pre->right!=NULL&&pre->right!=curr)pre=pre->right;// Make curr as the right child of its// inorder predecessor and move to // left node.if(pre->right==NULL){pre->right=curr;curr=curr->left;}// Revert the changes made in the 'if' part to// restore the original tree i.e., fix the right// child of predecessorelse{pre->right=NULL;if(n==1)returncurr->data;n--;curr=curr->right;}}}// If number of nodes is less than n.return-1;}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// hard coded binary tree.// 10// / \ // 20 30// / \ // 40 50structNode*root=createNode(10);root->left=createNode(20);root->right=createNode(30);root->left->left=createNode(40);root->left->right=createNode(50);intn=4;printf("%d\n",nthInorder(root,n));return0;}
Java
// Java program for nth node of inorder traversalsclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Given a binary tree, print its nth inorder node.staticintnthInorder(Noderoot,intn){Nodecurr=root;while(curr!=null){// if left child is null, check // curr node and move to right node.if(curr.left==null){if(n==1)returncurr.data;n--;curr=curr.right;}else{// Find the inorder predecessor of currNodepre=curr.left;while(pre.right!=null&&pre.right!=curr)pre=pre.right;// Make curr as the right child of its// inorder predecessor and move to // left node.if(pre.right==null){pre.right=curr;curr=curr.left;}// Revert the changes made in the 'if' part to// restore the original tree i.e., fix the right// child of predecessorelse{pre.right=null;if(n==1)returncurr.data;n--;curr=curr.right;}}}// If number of nodes is less than n.return-1;}publicstaticvoidmain(String[]args){// hard coded binary tree.// 10// / \// 20 30// / \// 40 50Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);intn=4;System.out.println(nthInorder(root,n));}}
Python
# Python program for nth node of inorder traversalsclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Given a binary tree, print its nth # inorder node.defnthInorder(root,n):curr=rootwhilecurr:# if left child is null, check # curr node and move to right node.ifcurr.leftisNone:ifn==1:returncurr.datan-=1curr=curr.rightelse:# Find the inorder predecessor of currpre=curr.leftwhilepre.rightisnotNone \
andpre.right!=curr:pre=pre.right# Make curr as the right child of its# inorder predecessor and move to # left node.ifpre.rightisNone:pre.right=currcurr=curr.leftelse:pre.right=Noneifn==1:returncurr.datan-=1curr=curr.right# If number of nodes is less than n.return-1if__name__=="__main__":# hard coded binary tree.# 10# / \# 20 30# / \# 40 50root=Node(10)root.left=Node(20)root.right=Node(30)root.left.left=Node(40)root.left.right=Node(50)n=4print(nthInorder(root,n))
C#
// C# program for nth node of inorder // traversalsusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Given a binary tree, print its nth inorder node.staticintnthInorder(Noderoot,intn){Nodecurr=root;while(curr!=null){// if left child is null, check // curr node and move to right node.if(curr.left==null){if(n==1)returncurr.data;n--;curr=curr.right;}else{// Find the inorder predecessor of currNodepre=curr.left;while(pre.right!=null&&pre.right!=curr)pre=pre.right;// Make curr as the right child of its// inorder predecessor and move to // left node.if(pre.right==null){pre.right=curr;curr=curr.left;}else{pre.right=null;if(n==1)returncurr.data;n--;curr=curr.right;}}}// If number of nodes is less than n.return-1;}staticvoidMain(string[]args){// hard coded binary tree.// 10// / \// 20 30// / \// 40 50Noderoot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);intn=4;Console.WriteLine(nthInorder(root,n));}}
JavaScript
// JavaScript program for nth node // of inorder traversalsclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Given a binary tree, print its // nth inorder node.functionnthInorder(root,n){letcurr=root;while(curr!==null){// if left child is null, check // curr node and move to right node.if(curr.left===null){if(n===1)returncurr.data;n--;curr=curr.right;}else{// Find the inorder predecessor of currletpre=curr.left;while(pre.right!==null&&pre.right!==curr)pre=pre.right;// Make curr as the right child of its// inorder predecessor and move to // left node.if(pre.right===null){pre.right=curr;curr=curr.left;}else{pre.right=null;if(n===1)returncurr.data;n--;curr=curr.right;}}}// If number of nodes is less than n.return-1;}// hard coded binary tree.// 10// / \// 20 30// / \// 40 50letroot=newNode(10);root.left=newNode(20);root.right=newNode(30);root.left.left=newNode(40);root.left.right=newNode(50);letn=4;console.log(nthInorder(root,n));