-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101-binary_tree_levelorder.c
More file actions
38 lines (34 loc) · 946 Bytes
/
101-binary_tree_levelorder.c
File metadata and controls
38 lines (34 loc) · 946 Bytes
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
#include "binary_trees.h"
/**
* levelorder - function that goes through a binary tree
* @tree: pointer to the node to tranverse.
* @func: is a pointer to a function to call for each node.
* Return: void
*/
void levelorder(const binary_tree_t *tree, void (*func)(int))
{
if (tree == NULL)
return;
if (tree->left != NULL)
func(tree->left->n);
if (tree->right != NULL)
func(tree->right->n);
levelorder(tree->left, func);
levelorder(tree->right, func);
}
/**
* binary_tree_levelorder - function that goes through a binary tree using
* level-order traversal
*
* @tree: is a pointer to the root node of the tree to traverse
*
* @func: is a pointer to a function to call for each node. The value in the
* node must be passed as a parameter to this function.
*/
void binary_tree_levelorder(const binary_tree_t *tree, void (*func)(int))
{
if (tree == NULL || func == NULL)
return;
func(tree->n);
levelorder(tree, func);
}