-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConnect_leaves_binary_tree.cpp
67 lines (60 loc) · 1.98 KB
/
Connect_leaves_binary_tree.cpp
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
59
60
61
62
63
64
65
66
67
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <cassert>
#include <iostream>
#include <list>
#include <memory>
#include <vector>
#include "./Binary_tree_prototype.h"
using std::cout;
using std::endl;
using std::list;
using std::unique_ptr;
using std::vector;
void connect_leaves_helper(const unique_ptr<BinaryTreeNode<int>>& T,
list<const unique_ptr<BinaryTreeNode<int>>*>* L);
// @include
list<const unique_ptr<BinaryTreeNode<int>>*> connect_leaves(
const unique_ptr<BinaryTreeNode<int>>& T) {
list<const unique_ptr<BinaryTreeNode<int>>*> L;
connect_leaves_helper(T, &L);
return L;
}
void connect_leaves_helper(const unique_ptr<BinaryTreeNode<int>>& T,
list<const unique_ptr<BinaryTreeNode<int>>*>* L) {
if (T) {
if (!T->left && !T->right) {
L->emplace_back(&T);
} else {
connect_leaves_helper(T->left, L);
connect_leaves_helper(T->right, L);
}
}
}
// @exclude
int main(int argc, char* argv[]) {
// 3
// 2 5
// 1 4 6
unique_ptr<BinaryTreeNode<int>> root = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{3, nullptr, nullptr});
root->left = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{2, nullptr, nullptr});
root->left->left = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{1, nullptr, nullptr});
root->right = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{5, nullptr, nullptr});
root->right->left = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{4, nullptr, nullptr});
root->right->right = unique_ptr<BinaryTreeNode<int>>(
new BinaryTreeNode<int>{6, nullptr, nullptr});
// should output 1, 4, 6
auto L = connect_leaves(root);
vector<int> output;
for (const auto* l : L) {
output.push_back((*l)->data);
cout << (*l)->data << endl;
}
assert(output.size() == 3);
assert(output[0] == 1 && output[1] == 4 && output[2] == 6);
return 0;
}