Given a binary tree and a value K. The task is to print the k-th node in the diagonal traversal of the binary tree. If no such node exists then print -1.
Examples:
Input :
8
/ \
3 10
/ / \
1 6 14
/ \ /
4 7 13
k = 5
Output : 6
Diagonal Traversal of the above tree is:
8 10 14
3 6 7 13
1 4
Input :
1
/ \
2 3
/ \
4 5
k = 7
Output : -1
Approach: The idea is to perform the diagonal traversal of the binary tree until K nodes are visited in the diagonal traversal. While traversing for each node visited decrement the value of variable K and return the current node when the value of K becomes zero. If the diagonal traversal does not contain at least K nodes, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* node = new Node();
node->data = data;
node->left = node->right = NULL;
return (node);
}
int diagonalPrint(Node* root, int k)
{
if (root == NULL || k == 0)
return -1;
int ans = -1;
queue<Node*> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp == NULL) {
if (q.empty()) {
if (k == 0)
return ans;
else
break ;
}
q.push(NULL);
}
else {
while (temp) {
if (k == 0)
return ans;
k--;
ans = temp->data;
if (temp->left)
q.push(temp->left);
temp = temp->right;
}
}
}
return -1;
}
int main()
{
Node* root = newNode(8);
root->left = newNode(3);
root->right = newNode(10);
root->left->left = newNode(1);
root->left->right = newNode(6);
root->right->right = newNode(14);
root->right->right->left = newNode(13);
root->left->right->left = newNode(4);
root->left->right->right = newNode(7);
int k = 9;
cout << diagonalPrint(root, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static int diagonalPrint(Node root, int k)
{
if (root == null || k == 0 )
return - 1 ;
int ans = - 1 ;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
q.add( null );
while (q.size() > 0 )
{
Node temp = q.peek();
q.remove();
if (temp == null )
{
if (q.size() == 0 )
{
if (k == 0 )
return ans;
else
break ;
}
q.add( null );
}
else {
while (temp != null )
{
if (k == 0 )
return ans;
k--;
ans = temp.data;
if (temp.left!= null )
q.add(temp.left);
temp = temp.right;
}
}
}
return - 1 ;
}
public static void main(String args[])
{
Node root = newNode( 8 );
root.left = newNode( 3 );
root.right = newNode( 10 );
root.left.left = newNode( 1 );
root.left.right = newNode( 6 );
root.right.right = newNode( 14 );
root.right.right.left = newNode( 13 );
root.left.right.left = newNode( 4 );
root.left.right.right = newNode( 7 );
int k = 9 ;
System.out.println( diagonalPrint(root, k));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data) :
node = Node( 0 )
node.data = data
node.left = node.right = None
return (node)
def diagonalPrint( root, k) :
if (root = = None or k = = 0 ) :
return - 1
ans = - 1
q = []
q.append(root)
q.append( None )
while ( len (q) > 0 ):
temp = q[ 0 ]
q.pop( 0 )
if (temp = = None ):
if ( len (q) = = 0 ) :
if (k = = 0 ) :
return ans
else :
break
q.append( None )
else :
while (temp ! = None ):
if (k = = 0 ) :
return ans
k = k - 1
ans = temp.data
if (temp.left ! = None ):
q.append(temp.left)
temp = temp.right
return - 1
root = newNode( 8 )
root.left = newNode( 3 )
root.right = newNode( 10 )
root.left.left = newNode( 1 )
root.left.right = newNode( 6 )
root.right.right = newNode( 14 )
root.right.right.left = newNode( 13 )
root.left.right.left = newNode( 4 )
root.left.right.right = newNode( 7 )
k = 9
print ( diagonalPrint(root, k))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static int diagonalPrint(Node root, int k)
{
if (root == null || k == 0)
return -1;
int ans = -1;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
q.Enqueue( null );
while (q.Count > 0)
{
Node temp = q.Peek();
q.Dequeue();
if (temp == null )
{
if (q.Count == 0)
{
if (k == 0)
return ans;
else
break ;
}
q.Enqueue( null );
}
else
{
while (temp != null )
{
if (k == 0)
return ans;
k--;
ans = temp.data;
if (temp.left!= null )
q.Enqueue(temp.left);
temp = temp.right;
}
}
}
return -1;
}
public static void Main(String []args)
{
Node root = newNode(8);
root.left = newNode(3);
root.right = newNode(10);
root.left.left = newNode(1);
root.left.right = newNode(6);
root.right.right = newNode(14);
root.right.right.left = newNode(13);
root.left.right.left = newNode(4);
root.left.right.right = newNode(7);
int k = 9;
Console.WriteLine( diagonalPrint(root, k));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function newNode(data)
{
var node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
function diagonalPrint(root, k)
{
if (root == null || k == 0)
return -1;
var ans = -1;
var q = [];
q.push(root);
q.push( null );
while (q.length > 0)
{
var temp = q[0];
q.shift();
if (temp == null )
{
if (q.length == 0)
{
if (k == 0)
return ans;
else
break ;
}
q.push( null );
}
else
{
while (temp != null )
{
if (k == 0)
return ans;
k--;
ans = temp.data;
if (temp.left!= null )
q.push(temp.left);
temp = temp.right;
}
}
}
return -1;
}
var root = newNode(8);
root.left = newNode(3);
root.right = newNode(10);
root.left.left = newNode(1);
root.left.right = newNode(6);
root.right.right = newNode(14);
root.right.right.left = newNode(13);
root.left.right.left = newNode(4);
root.left.right.right = newNode(7);
var k = 9;
document.write( diagonalPrint(root, k));
</script>
|
Time Complexity: O(N), where N is the total number of nodes in the binary tree.
Auxiliary Space: O(N)