【演算法面試題】尋找二叉搜尋樹中兩個節點的最近公用祖先節點

來源:互聯網
上載者:User

【演算法面試題】尋找二叉搜尋樹中兩個節點的最近公用祖先節點

http://geeksforgeeks.org/?p=1029

 

給定了一個二叉搜尋樹中任意的兩個節點的值,要你寫一個c/c++程式,去找到這兩個節點的最近公用祖先,你可以假定給定的值存在於二叉樹的某個節點中。

 

函式宣告:

 

int FindLowestCommonAncestor(node* root, int value1, int value)

 

 

 

 輸入: 4 和 14
 輸出: 8
 (4和14的共同祖先有{8, 20},其中8是最近的公用祖先節點)

演算法:

 

基本思想是:給定二叉樹中的兩個節點n1, n2(假定n1<n2), 其最近的公用祖先節點的值n應該滿足 n1<n<n2,所以我們可以前序走訪二叉搜尋樹,當發現一個節點的值在n1和n2之間時,則此節點為所求節點。如果節點的值大於n1和n2,則所求節點在當前節點的左子樹;否則在右子樹。

#include <stdio.h><br />#include <stdlib.h> </p><p>/* A binary tree node has data, pointer to left child<br /> and a pointer to right child */ </p><p>struct node<br />{<br /> int data;<br /> struct node* left;<br /> struct node* right;<br />}; </p><p>struct node* newNode(int ); </p><p>/* Function to find least comman ancestor of n1 and n2 */<br />int leastCommanAncestor(struct node* root, int n1, int n2)<br />{<br /> /* If we have reached a leaf node then LCA doesn't exist<br /> If root->data is equal to any of the inputs then input is<br /> not valid. For example 20, 22 in the given figure */ </p><p> if(root == NULL || root->data == n1 || root->data == n2)<br /> return -1; </p><p> /* If any of the input nodes is child of the current node<br /> we have reached the LCA. For example, in the above figure<br /> if we want to calculate LCA of 12 and 14, recursion should<br /> terminate when we reach 8*/<br /> if((root->right != NULL) &&<br /> (root->right->data == n1 || root->right->data == n2))<br /> return root->data; </p><p> if((root->left != NULL) &&<br /> (root->left->data == n1 || root->left->data == n2))<br /> return root->data; </p><p> if(root->data > n1 && root->data < n2)<br /> return root->data; </p><p> if(root->data > n1 && root->data > n2)<br /> return leastCommanAncestor(root->left, n1, n2); </p><p> if(root->data < n1 && root->data < n2)<br /> return leastCommanAncestor(root->right, n1, n2);<br />} </p><p>/* Helper function that allocates a new node with the<br /> given data and NULL left and right pointers. */<br />struct node* newNode(int data)<br />{<br /> struct node* node = (struct node*)<br /> malloc(sizeof(struct node));<br /> node->data = data;<br /> node->left = NULL;<br /> node->right = NULL;<br /> return(node);<br />} </p><p>/* Driver program to test mirror() */<br />int main()<br />{<br /> struct node *root = newNode(2);<br /> root->left = newNode(1);<br /> root->ight = newNode(4);<br /> root->right->left = newNode(3);<br /> root->right->right = newNode(5); </p><p>/* Constructed binary search tree is<br /> 2<br /> / /<br /> 1 4<br /> / /<br /> 3 5<br />*/ </p><p> printf("/n The Least Common Ancestor is /n");<br /> printf("%d", leastCommanAncestor(root, 3, 5)); </p><p> getchar();<br /> return 0;<br />}<br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.