-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-binary_trees_ancestor.c
54 lines (43 loc) · 1.15 KB
/
100-binary_trees_ancestor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "binary_trees.h"
/**
* binary_trees_ancestor - finds the lowest common ancestor of two nodes
* @first: pointer to the first node
* @second: pointert to the second node
*
* Return: pointer to the lowest common ancestor, or NULL otherwise
*/
binary_tree_t *binary_trees_ancestor(
const binary_tree_t *first, const binary_tree_t *second)
{
int depth_first, depth_second;
depth_first = binary_tree_depth(first);
depth_second = binary_tree_depth(second);
if (first == NULL || second == NULL)
return (NULL);
while (depth_first > depth_second)
{
first = first->parent;
depth_first--;
}
while (depth_second > depth_first)
{
second = second->parent;
depth_second--;
}
while (first != second && first != NULL && second != NULL)
{
first = first->parent;
second = second->parent;
}
return ((first == second) ? (binary_tree_t *)first : NULL);
}
/**
* binary_tree_depth - measures the depth of a node in a binary tree
* @tree: pointer to the target node
*
* Return: Depth of tree, otherwise 0
*/
size_t binary_tree_depth(const binary_tree_t *tree)
{
return ((tree && tree->parent) ? 1 + binary_tree_depth(tree->parent) : 0);
}