This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
struct Node
{
int value;
Node* right;
Node* left;
Node(int val) : value(val), left(nullptr), right(nullptr) {}
};
class Tree
{
public:
Node* root;
Tree(int height) {
root = nullptr;
int leafValue = 0;
build(root, height, leafValue);
}
void build(Node*& node, int height, int& leafValue) {
if(height == 0) return;
node = new Node(0);
build(node -> left, height -1, leafValue);
build(node -> right, height -1, leafValue);
if (height == 1) { leafValue++; node -> value = leafValue; }
}
int traverse(vector<int> path, int height) {
Node* current = root;
for (int i = 0; i < height; ++i) {
current = (path[i] == 0) ? current->left : current->right;
}
return current ? current -> value : -1;
}
};
int main()
{
int height, node;
cin >> height >> node;
int rows = 1 << height;
Tree tree(height + 1);
std::vector<int> path(height);
for (int i = 0; i < node; ++i) {
for (int j = 0; j < height; ++j) path[j] = (i >> j) & 1;
}
cout << tree.traverse(path, height) << endl;
return 0;
}
Compilation message (stderr)
rack.cpp: In constructor 'Node::Node(int)':
rack.cpp:12:11: warning: 'Node::left' will be initialized after [-Wreorder]
12 | Node* left;
| ^~~~
rack.cpp:11:11: warning: 'Node* Node::right' [-Wreorder]
11 | Node* right;
| ^~~~~
rack.cpp:14:5: warning: when initialized here [-Wreorder]
14 | Node(int val) : value(val), left(nullptr), right(nullptr) {}
| ^~~~
rack.cpp: In function 'int main()':
rack.cpp:53:9: warning: unused variable 'rows' [-Wunused-variable]
53 | int rows = 1 << height;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |