最低公共祖先 lowest common ancestor
2014-11-27 06:38
204 查看
最低公共祖先 lowest common ancestor
The idea is to traverse the tree starting from root. If any of the given keys (n1 and n2) matches with root, then root is LCA (assuming that bothkeys are present). If root doesn’t match with any of the keys, we recur for left and right subtree. The node which has one key present in its left subtree and the other key present in right subtree is the LCA. If both keys lie in left subtree, then left subtree
has LCA also, otherwise LCA lies in right subtree.
下面给出的算法的假设是,两个输入值都存在于二叉树中。如果有一个在,一个不在,那么函数返回值是那个存在的节点。
一个完整无误的算法,请看:http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/
/* Program to find LCA of n1 and n2 using one traversal of Binary Tree */ #include <iostream> using namespace std; // A Binary Tree Node struct Node { struct Node *left, *right; int key; }; // Utility function to create a new tree Node Node* newNode(int key) { Node *temp = new Node; temp->key = key; temp->left = temp->right = NULL; return temp; } // This function returns pointer to LCA of two given values n1 and n2. // This function assumes that n1 and n2 are present in Binary Tree struct Node *findLCA(struct Node* root, int n1, int n2) { // Base case if (root == NULL) return NULL; // If either n1 or n2 matches with root's key, report // the presence by returning root (Note that if a key is // ancestor of other, then the ancestor key becomes LCA if (root->key == n1 || root->key == n2) return root; // Look for keys in left and right subtrees Node *left_lca = findLCA(root->left, n1, n2); Node *right_lca = findLCA(root->right, n1, n2); // If both of the above calls return Non-NULL, then one key // is present in once subtree and other is present in other, // So this node is the LCA if (left_lca && right_lca) return root; // Otherwise check if left subtree or right subtree is LCA return (left_lca != NULL)? left_lca: right_lca; } // Driver program to test above functions int main() { // Let us create binary tree given in the above example 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)->key; cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6)->key; cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4)->key; cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4)->key; return 0; }
相关文章推荐
- PAT 1143—— Lowest Common Ancestor(二叉排序树 + 最低公共祖先)
- 236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)
- Lowest Common Ancestor of a Binary Search Tree(树中两个结点的最低公共祖先)
- 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)
- 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)
- [Java]寻找最低公共祖先(排序二叉树)Lowest Common Ancestor of a Binary Search Tree
- 最低公共祖先Lowest Common Ancestor of a Binary Tree
- 面试题50 树中两个节点的最低公共祖先LCA(Lowest Common Ancestor )
- [Java]寻找最低公共祖先(普通树的情形)Lowest Common Ancestor of a Binary Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最低公共祖先)
- LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)
- 第19题 在二叉查找树中找到两个结点的最低公共祖先 Lowest Common Ancestor
- 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最低公共祖先(难)
- 最近公共祖先 LCA (Lowest Common Ancestor)
- 最近公共祖先 (Lowest common ancestor)
- lintcode lowest-common-ancestor 最近公共祖先
- Lowest Common Ancestor of a Binary Search Tree 递归寻找二叉查找树公共祖先
- Range Minimum Query and Lowest Common Ancestor 区间最值查询和最近公共祖先问题
- Lowest Common Ancestor of a Binary Tree(二叉树公共祖先)
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先