您好,登錄后才能下訂單哦!
#pragma once #include <iostream> using namespace std; /**************** * 二叉樹中 找兩個結點的最近的公共祖先結點 ******************/ struct Node { Node* left; Node* right; int value; Node(int v) :value(v) ,left(NULL) ,right(NULL) {} };
// 方法一 int count 計數
int _find_ancestor(Node* root, Node* & ancestor, const Node* a, const Node* b) { if (root == NULL) { return 0; } int count = 0; count += _find_ancestor(root->left, ancestor, a , b); if (root == a || root == b) { count += 1; } /*if (count == 2) { ancestor = root; }*/ count += _find_ancestor(root->right, ancestor, a, b); if (count == 2) { ancestor = root ; count = 0; // 防止返回 時 上面count的值還是2 導致 ancestor不準確 被覆蓋 } return count; } void test_find_ancestor() { // 1 // 2 3 // 4 5 Node n1(1); Node n2(2); Node n3(3); Node n4(4); Node n5(5); n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; Node* ancestor = NULL; // 4 5 // _find_ancestor(&n1, ancestor,&n4, &n5); // 2 5 // _find_ancestor(&n1, ancestor,&n2, &n5); // 5 3 _find_ancestor(&n1, ancestor,&n5, &n3); }
// 方法二 bool判別
bool find_ancestor_bool(Node* root, Node* & ancestor, const Node* a, const Node* b) { if (root == NULL) { return false; } bool b_left = find_ancestor_bool(root->left, ancestor, a, b); bool b_right = find_ancestor_bool(root->right, ancestor, a, b); if (root == a || root == b) { if (b_left || b_right) { ancestor = root; } return true; } if (b_left && b_right) { ancestor = root; } return b_left || b_right; } void test_find_ancestor_bool() { // 1 // 2 3 // 4 5 Node n1(1); Node n2(2); Node n3(3); Node n4(4); Node n5(5); n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; Node* ancestor = NULL; // 4 5 //find_ancestor_bool(&n1, ancestor,&n4, &n5); // 2 5 // find_ancestor_bool(&n1, ancestor,&n2, &n5); // 5 3 // find_ancestor_bool(&n1, ancestor,&n5, &n3); // 1 5 find_ancestor_bool(&n1, ancestor,&n1, &n5); }
// 方法3 利用棧 數組或 鏈表 記錄 找到結點的 路徑 最后 遍歷兩個 棧(數組或鏈表) 找到第一次出現比相同的點的前一個 即為公共祖先
// 注意 如果當前不是 返回時 要將當前壓棧的 元素彈出 棧用引用傳入
// 這里以 vector 為例 方便遍歷
#include<vector> void find_ancestor_vector_R(Node* root, vector<Node*>& va,vector<Node*>& vb, Node* a, Node* b, Node* &ancestor) { if (root == NULL) { return; } va.push_back(root); vb.push_back(root); find_ancestor_vector_R(root->left, va, vb, a, b, ancestor); find_ancestor_vector_R(root->right, va, vb, a, b, ancestor); if (va.back() != a) { va.pop_back(); } if (vb.back() != b) { vb.pop_back(); } } Node* find_ancestor_vector(Node* root, Node* a, Node* b) { vector<Node*> va,vb; Node* ancestor = NULL; find_ancestor_vector_R(root, va, vb, a, b, ancestor); // 找va vb 的分叉點 之前的點 int i = 0; while (i < va.size() && i < vb.size() && va[i] == vb[i]) { ancestor = va[i]; i++; } return ancestor; } void test_find_ancestor_vector() { // 1 // 2 3 // 4 5 Node n1(1); Node n2(2); Node n3(3); Node n4(4); Node n5(5); n1.left = &n2; n1.right = &n3; n2.left = &n4; n2.right = &n5; Node* ancestor = NULL; // 4 5 ancestor = find_ancestor_vector(&n1, &n4, &n5); // 2 5 ancestor = find_ancestor_vector(&n1, &n2, &n5); // 5 3 ancestor = find_ancestor_vector(&n1, &n5, &n3); // 1 5 ancestor = find_ancestor_vector(&n1, &n1, &n5); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。