1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| using namespace std;
class BinaryTree { public: int value; BinaryTree *left = nullptr; BinaryTree *right = nullptr; BinaryTree *parent = nullptr;
BinaryTree(int value) { this->value = value; } }; void helper(BinaryTree* node, BinaryTree* target, BinaryTree** res, bool& search) { if(!node or *res != nullptr) return; helper(node->left, target, res, search); if(search) {*res = node; search = false;} if(target == node) search = true; helper(node->right, target, res, search); } BinaryTree *findSuccessor(BinaryTree *tree, BinaryTree *node) { BinaryTree* res = nullptr; bool search = false; helper(tree, node, &res, search); return res; }
|