fork download
  1. #include <iostream>
  2. using namespace std;
  3. class node
  4. {
  5. public:
  6. int data;
  7. node*left;
  8. node*right;
  9. node(int d)
  10. {
  11. data=d;
  12. left=NULL;
  13. right=NULL;
  14. }
  15. };
  16. node* buildTree()
  17. {
  18. int d;
  19. cin>>d;
  20. if(d==-1)
  21. {
  22. return NULL;
  23. }
  24. node *root=new node(d);
  25. root->left=buildTree();
  26. root->right=buildTree();
  27. return root;
  28. }
  29. void print(node *root)
  30. {
  31. if(root==NULL)
  32. {
  33. return;
  34. }
  35. cout<<root->data<<" ";
  36. print(root->left);
  37. print(root->right);
  38. }
  39. int main() {
  40. node* root=buildTree();
  41. print(root);
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 4296KB
stdin
3 4 -1 6 -1 -1 5 1 -1 -1 -1
stdout
3 4 6 5 1