Find n-th node in Postorder traversal of a Binary Tree
Last Updated :
28 Feb, 2023
Given a Binary tree and a number N, write a program to find the N-th node in the Postorder traversal of the given Binary tree.
Prerequisite: Tree Traversal
Examples:
Input : N = 4
11
/ \
21 31
/ \
41 51
Output : 31
Explanation: Postorder Traversal of given Binary Tree is 41 51 21 31 11,
so 4th node will be 31.
Input : N = 5
25
/ \
20 30
/ \ / \
18 22 24 32
Output : 32
The idea to solve this problem is to do postorder traversal of the given binary tree and keep track of the count of nodes visited while traversing the tree and print the current node when the count becomes equal to N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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 NthPostordernode( struct Node* root, int N)
{
static int flag = 0;
if (root == NULL)
return ;
if (flag <= N) {
NthPostordernode(root->left, N);
NthPostordernode(root->right, N);
flag++;
if (flag == N)
cout << root->data;
}
}
int main()
{
struct Node* root = createNode(25);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(18);
root->left->right = createNode(22);
root->right->left = createNode(24);
root->right->right = createNode(32);
int N = 6;
NthPostordernode(root, N);
return 0;
}
|
Java
public class NthNodePostOrder {
static int flag = 0 ;
public static void NthPostordernode(Node root, int N)
{
if (root == null )
return ;
if (flag <= N)
{
NthPostordernode(root.left, N);
NthPostordernode(root.right, N);
flag++;
if (flag == N)
System.out.print(root.data);
}
}
public static void main(String args[]) {
Node root = new Node( 25 );
root.left = new Node( 20 );
root.right = new Node( 30 );
root.left.left = new Node( 18 );
root.left.right = new Node( 22 );
root.right.left = new Node( 24 );
root.right.right = new Node( 32 );
int N = 6 ;
NthPostordernode(root, N);
}
}
class Node
{
int data;
Node left, right;
Node( int data)
{
this .data=data;
}
};
|
Python3
class createNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
flag = [ 0 ]
def NthPostordernode(root, N):
if (root = = None ):
return
if (flag[ 0 ] < = N[ 0 ]):
NthPostordernode(root.left, N)
NthPostordernode(root.right, N)
flag[ 0 ] + = 1
if (flag[ 0 ] = = N[ 0 ]):
print (root.data)
if __name__ = = '__main__' :
root = createNode( 25 )
root.left = createNode( 20 )
root.right = createNode( 30 )
root.left.left = createNode( 18 )
root.left.right = createNode( 22 )
root.right.left = createNode( 24 )
root.right.right = createNode( 32 )
N = [ 6 ]
NthPostordernode(root, N)
|
C#
using System;
public class NthNodePostOrder
{
public class Node
{
public int data;
public Node left, right;
public Node( int data)
{
this .data=data;
}
}
static int flag = 0;
static void NthPostordernode(Node root, int N)
{
if (root == null )
return ;
if (flag <= N)
{
NthPostordernode(root.left, N);
NthPostordernode(root.right, N);
flag++;
if (flag == N)
Console.Write(root.data);
}
}
public static void Main(String []args)
{
Node root = new Node(25);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(18);
root.left.right = new Node(22);
root.right.left = new Node(24);
root.right.right = new Node(32);
int N = 6;
NthPostordernode(root, N);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
var flag = 0;
function NthPostordernode(root, N)
{
if (root == null )
return ;
if (flag <= N)
{
NthPostordernode(root.left, N);
NthPostordernode(root.right, N);
flag++;
if (flag == N)
document.write(root.data);
}
}
var root = new Node(25);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(18);
root.left.right = new Node(22);
root.right.left = new Node(24);
root.right.right = new Node(32);
var N = 6;
NthPostordernode(root, N);
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
Where n is the number of nodes in the given binary tree.
- Auxiliary Space: O(h)
Where h is the height of the tree. The extra space used is due to the recursion function call stack. In the worst case (if the tree is skewed) this can go upto O(n).
Iterative Approach:
The idea to solve this problem is to do iterative postorder traversal of the given binary tree and keep track of the count of nodes visited while traversing the tree and print the current node when the count becomes equal to N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* createNode( int item)
{
Node* temp = new Node();
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
void Nthpostordernode(Node* root, int N)
{
if (!root)
return ;
int cnt = 0;
stack<Node*> s;
Node* curr = root;
while (!s.empty() || curr != NULL) {
while (curr) {
s.push(curr);
curr = curr->left;
}
Node* temp = s.top()->right;
if (!temp) {
temp = s.top();
s.pop();
cnt++;
if (cnt == N) {
cout << temp->data << " " ;
return ;
}
while (!s.empty() && s.top()->right == temp) {
temp = s.top();
s.pop();
cnt++;
if (cnt == N) {
cout << temp->data << " " ;
return ;
}
}
}
else
curr = temp;
}
}
int main()
{
Node* root = createNode(25);
root->left = createNode(20);
root->right = createNode(30);
root->left->left = createNode(18);
root->left->right = createNode(22);
root->right->left = createNode(24);
root->right->right = createNode(32);
int N = 6;
Nthpostordernode(root, N);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class NthPostOrderNode {
static void findNthPostOrderNode(Node root, int N)
{
if (root == null ) {
return ;
}
int cnt = 0 ;
Stack<Node> s = new Stack<Node>();
Node curr = root;
while (!s.empty() || curr != null ) {
while (curr != null ) {
s.push(curr);
curr = curr.left;
}
Node temp = s.peek().right;
if (temp == null ) {
temp = s.pop();
cnt++;
if (cnt == N) {
System.out.print(temp.data + " " );
return ;
}
while (!s.empty()
&& s.peek().right == temp) {
temp = s.pop();
cnt++;
if (cnt == N) {
System.out.print(temp.data + " " );
return ;
}
}
}
else {
curr = temp;
}
}
}
public static void main(String[] args)
{
Node root = new Node( 25 );
root.left = new Node( 20 );
root.right = new Node( 30 );
root.left.left = new Node( 18 );
root.left.right = new Node( 22 );
root.right.left = new Node( 24 );
root.right.right = new Node( 32 );
int N = 6 ;
findNthPostOrderNode(root, N);
}
}
|
Python
class Node:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def createNode(item):
temp = Node(item)
return temp
def Nthpostordernode(root, N):
if (root is None ):
return
cnt = 0
s = []
curr = root
while ( len (s) > 0 or curr is not None ):
while (curr is not None ):
s.append(curr)
curr = curr.left
temp = s[ len (s) - 1 ].right
if (temp is None ):
temp = s.pop()
cnt = cnt + 1
if (cnt = = N):
print (temp.data)
return
while ( len (s) > 0 and s[ len (s) - 1 ].right = = temp):
temp = s.pop()
cnt + = 1
if (cnt = = N):
print (temp.data)
return
else :
curr = temp
root = createNode( 25 )
root.left = createNode( 20 )
root.right = createNode( 30 )
root.left.left = createNode( 18 )
root.left.right = createNode( 22 )
root.right.left = createNode( 24 )
root.right.right = createNode( 32 )
N = 6
Nthpostordernode(root, N)
|
C#
using System;
using System.Collections.Generic;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class NthPostOrderNode
{
static void FindNthPostOrderNode(Node root, int N)
{
if (root == null )
{
return ;
}
int cnt = 0;
Stack<Node> s = new Stack<Node>();
Node curr = root;
while (s.Count > 0 || curr != null )
{
while (curr != null )
{
s.Push(curr);
curr = curr.left;
}
Node temp = s.Peek().right;
if (temp == null )
{
temp = s.Pop();
cnt++;
if (cnt == N)
{
Console.Write(temp.data + " " );
return ;
}
while (s.Count > 0 && s.Peek().right == temp)
{
temp = s.Pop();
cnt++;
if (cnt == N)
{
Console.Write(temp.data + " " );
return ;
}
}
}
else
{
curr = temp;
}
}
}
public static void Main()
{
Node root = new Node(25);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(18);
root.left.right = new Node(22);
root.right.left = new Node(24);
root.right.right = new Node(32);
int N = 6;
FindNthPostOrderNode(root, N);
}
}
|
Javascript
class Node{
constructor(item){
this .data = item;
this .left = null ;
this .right = null ;
}
}
function createNode(item){
return new Node(item);
}
function Nthpostordernode(root, N){
if (root == null )
return ;
let cnt = 0;
let s = [];
let curr = root;
while (s.length > 0 || curr != null ){
while (curr != null ){
s.push(curr);
curr = curr.left;
}
let temp = s[s.length - 1].right;
if (temp == null ){
temp = s.pop();
cnt++;
if (cnt == N){
document.write(temp.data + " " ) ;
return ;
}
while (s.length > 0 && s[s.length-1].right == temp){
temp = s.pop();
cnt++;
if (cnt == N){
document.write(temp.data + " " );
return ;
}
}
}
else {
curr = temp;
}
}
}
let root = createNode(25);
root.left = createNode(20);
root.right = createNode(30);
root.left.left = createNode(18);
root.left.right = createNode(22);
root.right.left = createNode(24);
root.right.right = createNode(32);
let N = 6;
Nthpostordernode(root, N);
|
Complexity Analysis:
- Time Complexity: O(n)
Where n is the number of nodes in the given binary tree.
- Auxiliary Space: O(h)
Where h is the height of the tree. The extra space used is due to the recursion function call stack. In the worst case (if the tree is skewed) this can go upto O(n).
Share your thoughts in the comments
Please Login to comment...