Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.
Locked until you have predicted 3 outputs correctly.
| 1 | struct Node { |
| 2 | int val; |
| 3 | Node* left; |
| 4 | Node* right; |
| 5 | }; |
| 6 | |
| 7 | Node* build(const vector<optional<int>>& level) { |
| 8 | |
| 9 | if (level.empty() || !level[0]) { |
| 10 | return nullptr; |
| 11 | } |
| 12 | |
| 13 | Node* root = new Node{*level[0], nullptr, nullptr}; |
| 14 | |
| 15 | queue<Node*> q; |
| 16 | q.push(root); |
| 17 | |
| 18 | int i = 1; |
| 19 | int n = level.size(); |
| 20 | |
| 21 | while (!q.empty() && i < n) { |
| 22 | |
| 23 | Node* node = q.front(); |
| 24 | q.pop(); |
| 25 | |
| 26 | if (level[i]) { |
| 27 | node->left = new Node{*level[i], nullptr, nullptr}; |
| 28 | q.push(node->left); |
| 29 | } |
| 30 | |
| 31 | i++; |
| 32 | |
| 33 | if (i < n && level[i]) { |
| 34 | node->right = new Node{*level[i], nullptr, nullptr}; |
| 35 | q.push(node->right); |
| 36 | } |
| 37 | |
| 38 | i++; |
| 39 | } |
| 40 | |
| 41 | return root; |
| 42 | } |
| 43 | |
| 44 | Node* meet(Node* node, int p, int q) { |
| 45 | |
| 46 | while (node) { |
| 47 | |
| 48 | if (p < node->val && q < node->val) { |
| 49 | node = node->left; |
| 50 | |
| 51 | } else if (p > node->val && q > node->val) { |
| 52 | node = node->right; |
| 53 | |
| 54 | } else { |
| 55 | return node; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | int solution(const vector<optional<int>>& level, int p, int q) { |
| 63 | |
| 64 | Node* root = build(level); |
| 65 | Node* found = meet(root, p, q); |
| 66 | |
| 67 | if (!found) { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | return found->val; |
| 72 | } |
Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.