Given a binary tree and a value K, the task is to check if every node of the binary tree has either the value of the node as K or at least one of its adjacent connected nodes has the value K.
Examples:
Input:
1
/ \
0 0
/ \ \
1 0 1
/ / \ \
2 1 0 5
/ /
3 0
/
0
K = 0
Output: False
Explanation:
We can observe that some leaf nodes
are having value other than 0 and
are not connected with any
adjacent node whose value is 0.
Input:
0
/ \
2 1
\
0
K = 0
Output: True
Explanation:
Since, all nodes of the tree
are either having value 0 or
connected with adjacent node
having value 0.
Approach:
- The idea is to simply perform preorder traversal and pass the reference of parent node recursively.
- While traversing for each node check the following conditions:
- if the value of the node is K or,
- if its parent node value is K or,
- if any of its child node value is K.
- If any node of the tree doesn’t satisfy any of the given three conditions then, return False, else return True.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
struct node {
int value;
struct node *right, *left;
};
struct node* newnode( int key)
{
struct node* temp = new node;
temp->value = key;
temp->right = NULL;
temp->left = NULL;
return temp;
}
bool connectedK(node* root,
node* parent,
int K,
bool flag)
{
if (root->value != K) {
if (root->left == NULL
|| root->left->value != K) {
if (root->right == NULL
|| root->right->value != K) {
if (parent == NULL
|| parent->value != K) {
flag = false ;
return flag;
}
}
}
}
if (root->left != NULL) {
if (flag == true ) {
flag
= connectedK(root->left,
root, K, flag);
}
}
if (root->right != NULL) {
if (flag == true ) {
flag
= connectedK(root->right,
root, K, flag);
}
}
return flag;
}
int main()
{
struct node* root = newnode(0);
root->right = newnode(1);
root->right->right = newnode(0);
root->left = newnode(0);
int K = 0;
bool result
= connectedK(root, NULL,
K, true );
if (result == false )
cout << "False\n" ;
else
cout << "True\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class node
{
int value;
node right, left;
};
static node newnode( int key)
{
node temp = new node();
temp.value = key;
temp.right = null ;
temp.left = null ;
return temp;
}
static boolean connectedK(node root,
node parent,
int K,
boolean flag)
{
if (root.value != K)
{
if (root.left == null ||
root.left.value != K)
{
if (root.right == null ||
root.right.value != K)
{
if (parent == null ||
parent.value != K)
{
flag = false ;
return flag;
}
}
}
}
if (root.left != null )
{
if (flag == true )
{
flag = connectedK(root.left,
root, K, flag);
}
}
if (root.right != null )
{
if (flag == true )
{
flag = connectedK(root.right,
root, K, flag);
}
}
return flag;
}
public static void main(String[] args)
{
node root = newnode( 0 );
root.right = newnode( 1 );
root.right.right = newnode( 0 );
root.left = newnode( 0 );
int K = 0 ;
boolean result = connectedK(root, null ,
K, true );
if (result == false )
System.out.print( "False\n" );
else
System.out.print( "True\n" );
}
}
|
Python3
class Node:
def __init__( self ,key):
self .value = key
self .left = None
self .right = None
def connectedK(root, parent, K, flag):
if root.value ! = K:
if (root.left = = None or
root.left.value ! = K):
if (root.right = = None or
root.right.value ! = K):
if (parent = = None or
parent.value ! = K):
flag = False
return flag
if root.left ! = None :
if flag = = True :
flag = connectedK(root.left,
root, K, flag)
if root.right ! = None :
if flag = = True :
flag = connectedK(root.right,
root, K, flag)
return flag
root = Node( 0 )
root.right = Node( 1 )
root.right.right = Node( 0 )
root.left = Node( 0 )
K = 0
result = connectedK(root, None , K, True )
if result = = False :
print ( "False" )
else :
print ( "True" )
|
C#
using System;
class GFG{
class node
{
public int value;
public node right, left;
};
static node newnode( int key)
{
node temp = new node();
temp.value = key;
temp.right = null ;
temp.left = null ;
return temp;
}
static bool connectedK(node root,
node parent,
int K,
bool flag)
{
if (root.value != K)
{
if (root.left == null ||
root.left.value != K)
{
if (root.right == null ||
root.right.value != K)
{
if (parent == null ||
parent.value != K)
{
flag = false ;
return flag;
}
}
}
}
if (root.left != null )
{
if (flag == true )
{
flag = connectedK(root.left,
root, K, flag);
}
}
if (root.right != null )
{
if (flag == true )
{
flag = connectedK(root.right,
root, K, flag);
}
}
return flag;
}
public static void Main(String[] args)
{
node root = newnode(0);
root.right = newnode(1);
root.right.right = newnode(0);
root.left = newnode(0);
int K = 0;
bool result = connectedK(root, null ,
K, true );
if (result == false )
Console.Write( "False\n" );
else
Console.Write( "True\n" );
}
}
|
Javascript
<script>
class node
{
constructor(key) {
this .value = key;
this .left = this .right = null ;
}
}
function newnode(key)
{
let temp = new node(key);
return temp;
}
function connectedK(root, parent, K, flag)
{
if (root.value != K)
{
if (root.left == null ||
root.left.value != K)
{
if (root.right == null ||
root.right.value != K)
{
if (parent == null ||
parent.value != K)
{
flag = false ;
return flag;
}
}
}
}
if (root.left != null )
{
if (flag == true )
{
flag = connectedK(root.left, root, K, flag);
}
}
if (root.right != null )
{
if (flag == true )
{
flag = connectedK(root.right, root, K, flag);
}
}
return flag;
}
let root = newnode(0);
root.right = newnode(1);
root.right.right = newnode(0);
root.left = newnode(0);
let K = 0;
let result = connectedK(root, null , K, true );
if (result == false )
document.write( "False" );
else
document.write( "True" );
</script>
|
Output:
True
Time complexity: O(N), N is the number of nodes of the tree.
Auxiliary Space: O(1)