-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaf_node_list.h
More file actions
51 lines (45 loc) · 1.3 KB
/
Copy pathleaf_node_list.h
File metadata and controls
51 lines (45 loc) · 1.3 KB
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
#ifndef CPP_ALGORITHM_LEAF_NODE_LIST_H
#define CPP_ALGORITHM_LEAF_NODE_LIST_H
#include "binary_tree.h"
#include <vector>
namespace LeafNodeList
{
/**
* \brief Create a list of leaf nodes.
* \param root the root of the tree
* \return leaf node list
*/
std::vector<BinaryTree::Node<int>*> CreateLeafNodeList(
BinaryTree::Node<int>* root);
}
// ----------------------------------------------------------------------------
/**
* \brief Add leaf nodes to the list.
* \param root the root of the tree
* \param leaf_node_list leaf node list
*/
inline void AddLeafNodeToList(
BinaryTree::Node<int>* root,
std::vector<BinaryTree::Node<int>*>& leaf_node_list)
{
if (root == nullptr)
{
return;
}
if (root->left == nullptr && root->right == nullptr)
{
leaf_node_list.push_back(root);
return;
}
AddLeafNodeToList(root->left, leaf_node_list);
AddLeafNodeToList(root->right, leaf_node_list);
}
// ----------------------------------------------------------------------------
inline std::vector<BinaryTree::Node<int>*> LeafNodeList::CreateLeafNodeList(
BinaryTree::Node<int>* root)
{
auto leaf_node_list = std::vector<BinaryTree::Node<int>*>{};
AddLeafNodeToList(root, leaf_node_list);
return leaf_node_list;
}
#endif