fork download
  1. #include <vector>
  2. using namespace std;
  3.  
  4. struct TreeNode {
  5. int val;
  6. TreeNode *left, *right;
  7. inline TreeNode(int x = 0, TreeNode *l = nullptr, TreeNode *r = nullptr) :
  8. val(x), left(l), right(r) {} };
  9.  
  10. class Solution {
  11. static constexpr int M = 1e5+1;
  12. using ivector = vector<int>;
  13. using imatrix = vector<ivector>;
  14. using node_pointer = TreeNode*;
  15. public:
  16. inline node_pointer createBinaryTree(imatrix &descriptions) {
  17. vector<node_pointer> node(M,nullptr);
  18. vector<bool> has_parent(M,false);
  19. for (auto desc: descriptions) {
  20. const int parent = desc[0];
  21. const int child = desc[1];
  22. const int is_left = desc[2];
  23. if (node[parent] == nullptr)
  24. node[parent] = new TreeNode(parent);
  25. if (node[child] == nullptr)
  26. node[child] = new TreeNode(child);
  27. if (has_parent[child] = true, is_left)
  28. node[parent]->left = node[child];
  29. else
  30. node[parent]->right = node[child]; }
  31. int root = 1;
  32. while (has_parent[root] or node[root] == nullptr)
  33. ++root;
  34. return node[root]; } };
  35.  
  36. int main() {
  37. return 0; }
  38.  
Success #stdin #stdout 0s 5404KB
stdin
Standard input is empty
stdout
Standard output is empty