寻找二叉树两个节点的最低公共祖先(LCA)

来源:互联网 发布:淘宝网猎狐者户外用品 编辑:程序博客网 时间:2024/05/16 08:13

题意:给定一棵树,同时给出树中的两个结点(n1和n2),求它们的最低公共祖先。也就是常见的LCA(Lowest Common Ancestor )问题。

// O(n) 解决 LCA#include <iostream>#include <vector>using namespace std;//二叉树节点struct Node{    int key;    struct Node *left, *right;};//公用函数,生成一个节点Node * newNode(int k){    Node *temp = new Node;    temp->key = k;    temp->left = temp->right = NULL;    return temp;}//找到从root到 节点值为key的路径,存储在path中。没有的话返回-1bool findpath(Node * root,vector<int> &path,int key){if(root == NULL) return false;path.push_back(root->key);if(root->key == key) return true;//左子树或右子树 是否找到,找到的话当前节点就在路径中了bool find =  ( findpath(root->left, path, key) || findpath(root->right,path ,key) );if(find) return true;//该节点下未找到就弹出path.pop_back();return false;}int findLCA(Node * root,int key1,int key2){vector<int> path1,path2;bool find1 = findpath(root, path1, key1);bool find2 = findpath(root, path2, key2);if(find1 && find2){int ans ;for(int i=0; i<path1.size(); i++){if(path1[i] != path2[i]){break;}elseans = path1[i];}return ans;}return -1;}// Driver program to test above functionsint main(){    // 按照上面的图来创创建树    Node * root = newNode(1);    root->left = newNode(2);    root->right = newNode(3);    root->left->left = newNode(4);    root->left->right = newNode(5);    root->right->left = newNode(6);    root->right->right = newNode(7);    cout << "LCA(4, 5) = " << findLCA(root, 4, 5);    cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6);    cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4);    cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4);    return 0;}




0 0