編程演算法 - 二叉樹的最低公用祖先 代碼(C)

來源:互聯網
上載者:User

標籤:http   tla   als   spi   pre   include   path   eclipse   bin   

二叉樹的最低公用祖先 代碼(C)


本文地址: http://blog.csdn.net/caroline_wendy


二叉樹的最低公用祖先(lowest common ancestor), 首先先序遍曆找到兩個結點的路徑, 然後依據鏈表路徑找到最低的公用祖先.


代碼:

/* * main.cpp * *  Created on: 2014.6.12 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include <iostream>#include <list>#include <queue>using namespace std;struct BinaryTreeNode {BinaryTreeNode(int _value) {value = _value;left = NULL;right = NULL;}int value;BinaryTreeNode* left;BinaryTreeNode* right;};void printTree (BinaryTreeNode* tree){BinaryTreeNode* node = tree;std::queue<BinaryTreeNode*> temp1;std::queue<BinaryTreeNode*> temp2;temp1.push(node);while (!temp1.empty()){node = temp1.front();if (node->left != NULL) {temp2.push(node->left);}if (node->right != NULL) {temp2.push(node->right);}temp1.pop();std::cout << node->value  << " ";if (temp1.empty()){std::cout << std::endl;temp1 = temp2;std::queue<BinaryTreeNode*> empty;std::swap(temp2, empty);}}}BinaryTreeNode* buildTree (void){BinaryTreeNode* root = new BinaryTreeNode(1);BinaryTreeNode* node2 = new BinaryTreeNode(2);BinaryTreeNode* node3 = new BinaryTreeNode(3);BinaryTreeNode* node4 = new BinaryTreeNode(4);BinaryTreeNode* node5 = new BinaryTreeNode(5);BinaryTreeNode* node6 = new BinaryTreeNode(6);BinaryTreeNode* node7 = new BinaryTreeNode(7);BinaryTreeNode* node8 = new BinaryTreeNode(8);BinaryTreeNode* node9 = new BinaryTreeNode(9);BinaryTreeNode* node10 = new BinaryTreeNode(10);root->left = node2;root->right = node3;node2->left = node4;node2->right = node5;node4->left = node6;node4->right = node7;node5->left = node8;node5->right = node9;node9->left = node10;return root;}bool GetNodePath(BinaryTreeNode* root, int v, vector<BinaryTreeNode*>& path) {if (root->value == v)return true;path.push_back(root);bool found = false;if (root->left != NULL && !found)found = GetNodePath(root->left, v, path);if (root->right != NULL && !found)found = GetNodePath(root->right, v, path);if (!found)path.pop_back();return found;}BinaryTreeNode* GetLastCommonNode (const vector<BinaryTreeNode*>& path1, const vector<BinaryTreeNode*>& path2){vector<BinaryTreeNode*>::const_iterator it1 = path1.begin();vector<BinaryTreeNode*>::const_iterator it2 = path2.begin();BinaryTreeNode* pLast = NULL;while (it1 != path1.end() && it2 != path2.end()) {if ((*it1)->value == (*it2)->value)pLast = *it1;it1++;it2++;}return pLast;}BinaryTreeNode* GetLastCommonParent(BinaryTreeNode* root, int v1, int v2){if (root == NULL)return NULL;vector<BinaryTreeNode*> path1;GetNodePath(root, v1, path1);vector<BinaryTreeNode*> path2;GetNodePath(root, v2, path2);return GetLastCommonNode(path1, path2);}int main (void){BinaryTreeNode* root = buildTree();int v1 = 6;int v2 = 10;BinaryTreeNode* common = GetLastCommonParent(root, v1, v2);cout << "common node : " << common->value << endl;return 0;}


輸出:

common node : 2







編程演算法 - 二叉樹的最低公用祖先 代碼(C)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.