Advertisement
Derga

Untitled

Sep 18th, 2023
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. int tree_size;
  2.     cin >> tree_size;
  3.  
  4.     vector <Node> tree(tree_size + 1);
  5.     vector <bool> is_root(tree_size + 1, true);
  6.     is_root[0] = false;
  7.  
  8.     for (int i = 1; i <= tree_size; ++i) {
  9.         int left, right;
  10.         cin >> left >> right;
  11.         is_root[left] = false;
  12.         is_root[right] = false;
  13.  
  14.         tree[i].val = i;
  15.         if (left == 0) {
  16.             tree[i].left = nullptr;
  17.         } else {
  18.             tree[i].left = &tree[left];
  19.         }
  20.  
  21.         if (right == 0) {
  22.             tree[i].right = nullptr;
  23.         } else {
  24.             tree[i].right = &tree[right];
  25.         }
  26.     }
  27.  
  28.     Node* root = nullptr;
  29.     for (int i = 1; i < is_root.size(); ++i) {
  30.         if (is_root[i]) {
  31.             root = &tree[i];
  32.             break;
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement