Даны два BST
(двоичные деревья поиска). Как найти наибольшее общее поддерево в данных двух двоичных деревьях
?
РЕДАКТИРОВАТЬ 1: Вот что я подумал:
Пусть, r1 = текущий узел 1-го дерева r2 = текущий узел 2-го дерева
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
Я могу думать о случаях, которые нам нужно проверить, но пока я не могу их закодировать. И это НЕ домашнее задание. Как это выглядит?