Check if two Binary trees are identical after exactly K changes
Given two binary trees T1 and T2 and integer K, the task is to check whether both trees are identical or not after making exactly K changes in the first tree. In each change, one tree element can be converted into any other integer.
Examples:
Input: K = 1
T1 = 1 T2 = 1
/ \ / \
2 4 2 3
Output: Yes
Explanation: Change the node with value 4 to value 3
Input: K = 5
T1 = 1 T2 = 1
/ \ / \
2 5 3 5
/ \ / \ / \ / \
7 9 10 11 7 6 10 21
Output: Yes
Explanation: In the above two tree there are 3 node value which are different in first tree i.e, 2, 9, 11.
Hence we make atleast 3 changes to convert them to 3, 6, 21 respectively to make tree identical.
After 3 changes left K = 5 – 3 = 2 changes.
Utilize these two changes by converting any node to any random one then convert back to original one.
In this way we have exactly K = 5 changes.
Approach: The problem is solved using an iterative approach using a stack.
- We check all the respective nodes of both the trees one by one with the help of iterative inorder traversal.
- If we found that the respective data parts of both the trees are not matching then we simply increment the count.
- If the structure of both the trees is different, return false.
- At the end we check if our count is less than K and (k-count) is even or not if so then return true else return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
int data;
node* left;
node* right;
};
node* newNode( int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
bool checkIdentical(node* p, node* q, int k)
{
stack<node *> st1, st2;
int count = 0;
while (p || !st1.empty() && q || !st2.empty()) {
if (p && q) {
st1.push(p);
st2.push(q);
p = p->left;
q = q->left;
}
else if (p && !q || !p && q)
return 0;
else {
p = st1.top();
q = st2.top();
st1.pop();
st2.pop();
if (p->data != q->data)
count++;
p = p->right;
q = q->right;
}
}
if (count <= k && (k - count) % 2 == 0)
return 1;
else
return 0;
}
int main()
{
node* root1 = newNode(1);
root1->left = newNode(2);
root1->right = newNode(4);
node* root2 = newNode(1);
root2->left = newNode(2);
root2->right = newNode(3);
int K = 1;
if (checkIdentical(root1, root2, K))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
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 boolean checkIdentical(node p, node q, int k)
{
Stack<node > st1 = new Stack<>();
Stack<node > st2 = new Stack<>();
int count = 0 ;
while (p!= null || (st1.isEmpty()) && q!= null || !st2.isEmpty()) {
if (p!= null && q!= null ) {
st1.add(p);
st2.add(q);
p = p.left;
q = q.left;
}
else if (p!= null && q== null || p== null && q!= null )
return false ;
else {
p = st1.peek();
q = st2.peek();
st1.pop();
st2.pop();
if (p.data != q.data)
count++;
p = p.right;
q = q.right;
}
}
if (count <= k && (k - count) % 2 == 0 )
return true ;
else
return false ;
}
public static void main(String[] args)
{
node root1 = newNode( 1 );
root1.left = newNode( 2 );
root1.right = newNode( 4 );
node root2 = newNode( 1 );
root2.left = newNode( 2 );
root2.right = newNode( 3 );
int K = 1 ;
if (checkIdentical(root1, root2, K))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
class node:
def __init__( self , d):
self .data = d
self .left = None
self .right = None
def newNode(data):
Node = node(data);
Node.left = None ;
Node.right = None ;
return Node
def checkIdentical(p, q, k):
st1 = []
st2 = []
count = 0
while (p or ( len (st1) > 0 ) and q or ( len (st2) > 0 )):
if (p and q):
st1.append(p)
st2.append(q)
p = p.left
q = q.left
elif (p and ( not q) or ( not p) and q):
return 0
else :
p = st1.pop()
st1.append(p)
q = st2.pop()
st2.append(q)
st1.pop()
st2.pop()
if (p.data ! = q.data):
count + = 1
p = p.right
q = q.right
if (count < = k and (k - count) % 2 = = 0 ):
return 1
else :
return 0
if __name__ = = '__main__' :
root1 = newNode( 1 );
root1.left = newNode( 2 );
root1.right = newNode( 4 );
root2 = newNode( 1 );
root2.left = newNode( 2 );
root2.right = newNode( 3 );
K = 1 ;
if (checkIdentical(root1, root2, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public
class node {
public int data;
public node left;
public node right;
};
static node newNode( int data)
{
node Node = new node();
Node.data = data;
Node.left = null ;
Node.right = null ;
return (Node);
}
static bool checkIdentical(node p, node q, int k)
{
Stack<node > st1 = new Stack<node>();
Stack<node > st2 = new Stack<node>();
int count = 0;
while (p!= null || (st1.Count!=0) && q!= null || st2.Count!=0) {
if (p!= null && q!= null ) {
st1.Push(p);
st2.Push(q);
p = p.left;
q = q.left;
}
else if (p!= null && q== null || p== null && q!= null )
return false ;
else {
p = st1.Peek();
q = st2.Peek();
st1.Pop();
st2.Pop();
if (p.data != q.data)
count++;
p = p.right;
q = q.right;
}
}
if (count <= k && (k - count) % 2 == 0)
return true ;
else
return false ;
}
public static void Main(String[] args)
{
node root1 = newNode(1);
root1.left = newNode(2);
root1.right = newNode(4);
node root2 = newNode(1);
root2.left = newNode(2);
root2.right = newNode(3);
int K = 1;
if (checkIdentical(root1, root2, K))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
let root1, root2;
function checkIdentical(p, q, k) {
let st1 = [];
let st2 = [];
let count = 0;
while (p || st1.length != 0 && q || st2.length !== 0) {
if (p && q) {
st1.push(p);
st2.push(q);
p = p.left;
q = q.left;
}
else if (p && !q || !p && q) return false ;
else {
p = st1[0];
q = st2[0];
st1.shift();
st2.shift();
if (p.data !== q.data) count++;
p = p.right;
q = q.right;
}
}
if (count <= k && (k - count) % 2 === 0) return true ;
else return false ;
}
root1 = new Node(1);
root1.left = new Node(2);
root1.right = new Node(4);
root2 = new Node(1);
root2.left = new Node(2);
root2.right = new Node(3);
let k = 1;
if (checkIdentical(root1, root2, k)) console.log( "Yes" );
else console.log( "No" );
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...