Даны два BSTs
(Деревья двоичного поиска). Как найти наибольшее общее поддерево в данных двух binary trees
?
РЕДАКТИРОВАТЬ 1: Вот что я подумал:
Пусть, r1 = текущий node 1-го дерева r2 = текущий node второго дерева
There are some of the cases I think we need to consider:
Case 1 : r1.data < r2.data
2 subproblems to solve:
first, check r1 and r2.left
second, check r1.right and r2
Case 2 : r1.data > r2.data
2 subproblems to solve:
- first, check r1.left and r2
- second, check r1 and r2.right
Case 3 : r1.data == r2.data
Again, 2 cases to consider here:
(a) current node is part of largest common BST
compute common subtree size rooted at r1 and r2
(b)current node is NOT part of largest common BST
2 subproblems to solve:
first, solve r1.left and r2.left
second, solve r1.right and r2.right
Я могу подумать о случаях, которые нам нужно проверить, но я не могу его закодировать, на данный момент. И это НЕ домашняя проблема. Это похоже?