您好,登錄后才能下訂單哦!
#pragma once #include <iostream> using namespace std; /**************** * 二叉樹中 找兩個結(jié)點的最近的公共祖先結(jié)點 ******************/ struct Node { Node* left; Node* right; int value; Node(int v) :value(v) ,left(NULL) ,right(NULL) {} };
// 方法一 int count 計數(shù)
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 導(dǎo)致 ancestor不準(zhǔn)確 被覆蓋 } 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 利用棧 數(shù)組或 鏈表 記錄 找到結(jié)點的 路徑 最后 遍歷兩個 棧(數(shù)組或鏈表) 找到第一次出現(xiàn)比相同的點的前一個 即為公共祖先
// 注意 如果當(dāng)前不是 返回時 要將當(dāng)前壓棧的 元素彈出 棧用引用傳入
// 這里以 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); }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。