fork(1) download
  1. #include <cstdio>
  2. #include <queue>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. struct bintree {
  8. int val;
  9. struct bintree *left, *right;
  10. };
  11.  
  12. int main(){
  13. struct bintree bt;
  14. bt.val = 1;
  15. bt.left = new struct bintree();
  16. bt.right = new struct bintree();
  17. //bt.left->left = bt.left->right =NULL;
  18. bt.left->val = 10000;
  19.  
  20. //bt.left->right = bt.right->right =NULL;
  21. bt.right->val = 100;
  22.  
  23. bt.left->right = new struct bintree();
  24. bt.left->right->val = 10;
  25.  
  26.  
  27. queue<struct bintree*> que;
  28. map<struct bintree*,bool> visited;
  29. que.push(&bt);
  30.  
  31. int sum=0;
  32. while(!que.empty()){
  33. struct bintree* bt = que.front();
  34. sum += bt->val;
  35. que.pop();
  36.  
  37. if(bt->left != NULL && visited.find(bt->left) == visited.end()){
  38. visited[bt->left] = true;
  39. que.push(bt->left);
  40. }
  41.  
  42. if(bt->right != NULL && visited.find(bt->right) == visited.end()){
  43. visited[bt->right] = true;
  44. que.push(bt->right);
  45. }
  46. }
  47.  
  48. printf("%d\n",sum);
  49. }
Success #stdin #stdout 0.02s 2860KB
stdin
Standard input is empty
stdout
10111