-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathBinary_tree_level_order.cc
82 lines (74 loc) · 2.43 KB
/
Binary_tree_level_order.cc
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2015 Elements of Programming Interviews. All rights reserved.
#include <cassert>
#include <iostream>
#include <memory>
#include <queue>
#include <vector>
#include "./Binary_tree_prototype.h"
using std::cout;
using std::endl;
using std::equal;
using std::make_unique;
using std::move;
using std::queue;
using std::unique_ptr;
using std::vector;
// @include
vector<vector<int>> BinaryTreeDepthOrder(
const unique_ptr<BinaryTreeNode<int>>& tree) {
queue<BinaryTreeNode<int>*> curr_depth_nodes({tree.get()});
vector<vector<int>> result;
while (!curr_depth_nodes.empty()) {
queue<BinaryTreeNode<int>*> next_depth_nodes;
vector<int> this_level;
while (!curr_depth_nodes.empty()) {
auto curr = curr_depth_nodes.front();
curr_depth_nodes.pop();
if (curr) {
this_level.emplace_back(curr->data);
// Defer the null checks to the null test above.
next_depth_nodes.emplace(curr->left.get());
next_depth_nodes.emplace(curr->right.get());
}
}
if (!this_level.empty()) {
result.emplace_back(this_level);
}
curr_depth_nodes = next_depth_nodes;
}
return result;
}
// @exclude
int main(int argc, char* argv[]) {
// 3
// 2 5
// 1 4 6
// 10
// 13
unique_ptr<BinaryTreeNode<int>> tree = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{3, nullptr, nullptr});
tree->left = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{2, nullptr, nullptr});
tree->left->left = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{1, nullptr, nullptr});
tree->left->left->left = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{10, nullptr, nullptr});
tree->left->left->left->right = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{13, nullptr, nullptr});
tree->right = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{5, nullptr, nullptr});
tree->right->left = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{4, nullptr, nullptr});
tree->right->right = make_unique<BinaryTreeNode<int>>(
BinaryTreeNode<int>{6, nullptr, nullptr});
// should output 3
// 2 5
// 1 4 6
// 10
// 13
auto result = BinaryTreeDepthOrder(tree);
vector<vector<int>> golden_res = {{3}, {2, 5}, {1, 4, 6}, {10}, {13}};
assert(equal(golden_res.begin(), golden_res.end(), result.begin(),
result.end()));
return 0;
}