-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-long_binary_trees_ancestor.c
executable file
·58 lines (50 loc) · 1.51 KB
/
100-long_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
55
56
57
58
#include "binary_trees.h"
size_t depth(const binary_tree_t *tree);
/**
* binary_trees_ancestor - Function finds the lowest common ancestor of two nodes
* @first: First node's pointer
* @second: Second node's pointer
*
* Return: If no common ancestors return NULL,
* Otherwise the common ancestor
*/
binary_tree_t *binary_trees_ancestor(const binary_tree_t *first,
const binary_tree_t *second)
{
size_t first_depth, scnd_depth;
if (first == NULL || second == NULL)
return (NULL);
if (first == second)
return ((binary_tree_t *)first);
if (first->parent == second->parent)
return ((binary_tree_t *)first->parent);
if (first == second->parent)
return ((binary_tree_t *)first);
if (first->parent == second)
return ((binary_tree_t *)second);
for (first_depth = depth(first); first_depth > 1; first_depth--)
first = first->parent;
for (scnd_depth = depth(second); scnd_depth > 1; scnd_depth--)
second = second->parent;
if (first == second)
return ((binary_tree_t *)first);
if (first->parent == second->parent)
return ((binary_tree_t *)first->parent);
if (first == second->parent)
return ((binary_tree_t *)first);
if (first->parent == second)
return ((binary_tree_t *)second);
else
return (NULL);
}
/**
* depth - Function measures the depth of a node in a binary tree
* @tree: Pointer to the node to measure the depth
*
* Return: If tree is NULL - (0),
* Otherwise the depth
*/
size_t depth(const binary_tree_t *tree)
{
return ((tree->parent != NULL) ? 1 + depth(tree->parent) : 0);
}