检查两个二叉树在恰好 K 变化后是否相同
给定两棵二叉树T1和T2以及整数K ,任务是在对第一棵树进行K 次更改后检查两棵树是否相同。在每次更改中,一个树元素可以转换为任何其他整数。
例子:
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.
方法: 使用堆栈的迭代方法解决了该问题。
- 我们在迭代中序遍历的帮助下一一检查两棵树的所有各自节点。
- 如果我们发现两棵树的各自数据部分不匹配,那么我们只需增加计数。
- 如果两棵树的结构不同,则返回 false。
- 最后,我们检查我们的计数是否小于 K 并且 (k-count) 是否为偶数,如果是,则返回 true,否则返回 false。
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// A binary tree node has data,
// pointer to left child
// and a pointer to right child
class node {
public:
int data;
node* left;
node* right;
};
// Function to create new node.
node* newNode(int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
// Given two trees, return
// true if they are
// convertible to identical
bool checkIdentical(node* p, node* q, int k)
{
stack st1, st2;
int count = 0;
while (p || !st1.empty() && q || !st2.empty()) {
// If p are q are not null
// push in stack
if (p && q) {
st1.push(p);
st2.push(q);
// Send p and q to its left child
p = p->left;
q = q->left;
}
// when one of them is null means
// they have different
// structure return false
else if (p && !q || !p && q)
return 0;
// If p and q are null
// pop node from stack
else {
p = st1.top();
q = st2.top();
st1.pop();
st2.pop();
// If data not match increment count
if (p->data != q->data)
count++;
// Send p and q to its right child
p = p->right;
q = q->right;
}
}
if (count <= k && (k - count) % 2 == 0)
return 1;
else
return 0;
}
// Driver code
int main()
{
/* 1 1
/ \ / \
2 4 2 3
*/
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
// Java code to implement the above approach
import java.util.*;
class GFG{
// A binary tree node has data,
// pointer to left child
// and a pointer to right child
static class node {
int data;
node left;
node right;
};
// Function to create new node.
static node newNode(int data)
{
node Node = new node();
Node.data = data;
Node.left = null;
Node.right = null;
return (Node);
}
// Given two trees, return
// true if they are
// convertible to identical
static boolean checkIdentical(node p, node q, int k)
{
Stack st1 = new Stack<>();
Stack st2 = new Stack<>();
int count = 0;
while (p!=null || (st1.isEmpty()) && q!=null || !st2.isEmpty()) {
// If p are q are not null
// push in stack
if (p!=null && q!=null) {
st1.add(p);
st2.add(q);
// Send p and q to its left child
p = p.left;
q = q.left;
}
// when one of them is null means
// they have different
// structure return false
else if (p!=null && q==null || p==null && q!=null)
return false;
// If p and q are null
// pop node from stack
else {
p = st1.peek();
q = st2.peek();
st1.pop();
st2.pop();
// If data not match increment count
if (p.data != q.data)
count++;
// Send p and q to its right child
p = p.right;
q = q.right;
}
}
if (count <= k && (k - count) % 2 == 0)
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
/* 1 1
/ \ / \
2 4 2 3
*/
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");
}
}
// This code contributed by shikhasingrajput
Yes
时间复杂度: O(N)
辅助空间: O(N)