fork download
  1. #pragma GCC optimize ("Ofast")
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. template<class S, class T> inline S chmax(S &a, T b){
  5. if(a<b){
  6. a=b;
  7. }
  8. return a;
  9. }
  10. #define main dummy_main
  11. int main(){
  12. return 0;
  13. }
  14. #undef main
  15. #define TreeNode dummy_TreeNode
  16. struct TreeNode{
  17. int val;
  18. TreeNode *left;
  19. TreeNode *right;
  20. TreeNode(int x) : val(x), left(NULL), right(NULL){
  21. }
  22. }
  23. ;
  24. #undef TreeNode
  25. int res;
  26. void solve(TreeNode *n, int now, int dir){
  27. if(n == NULL){
  28. return;
  29. }
  30. chmax(res, now);
  31. if(dir==1){
  32. solve(n->left,now+1, 0);
  33. }
  34. else{
  35. solve(n->left,1, 0);
  36. }
  37. if(dir==0){
  38. solve(n->right,now+1, 1);
  39. }
  40. else{
  41. solve(n->right,1, 1);
  42. }
  43. }
  44. class Solution{
  45. public:
  46. int longestZigZag(TreeNode* root){
  47. res = 0;
  48. solve(root, 0, -1);
  49. return res;
  50. }
  51. }
  52. ;
  53. // cLay varsion 20200308-1
  54.  
  55. // --- original code ---
  56. // #define main dummy_main
  57. // {}
  58. // #undef main
  59. //
  60. // #define TreeNode dummy_TreeNode
  61. // struct TreeNode {
  62. // int val;
  63. // TreeNode *left;
  64. // TreeNode *right;
  65. // TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  66. // };
  67. // #undef TreeNode
  68. //
  69. // int res;
  70. //
  71. // void solve(TreeNode *n, int now, int dir){
  72. // if(n == NULL) return;
  73. // res >?= now;
  74. // solve(n->left, if[dir==1, now+1, 1], 0);
  75. // solve(n->right, if[dir==0, now+1, 1], 1);
  76. // }
  77. //
  78. // class Solution {
  79. // public:
  80. // int longestZigZag(TreeNode* root) {
  81. // res = 0;
  82. // solve(root, 0, -1);
  83. // return res;
  84. // }
  85. // };
  86.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:26:12: error: variable or field ‘solve’ declared void
 void solve(TreeNode *n, int now, int dir){
            ^~~~~~~~
prog.cpp:26:12: error: ‘TreeNode’ was not declared in this scope
prog.cpp:26:12: note: suggested alternative: ‘remove’
 void solve(TreeNode *n, int now, int dir){
            ^~~~~~~~
            remove
prog.cpp:26:22: error: ‘n’ was not declared in this scope
 void solve(TreeNode *n, int now, int dir){
                      ^
prog.cpp:26:22: note: suggested alternative: ‘yn’
 void solve(TreeNode *n, int now, int dir){
                      ^
                      yn
prog.cpp:26:25: error: expected primary-expression before ‘int’
 void solve(TreeNode *n, int now, int dir){
                         ^~~
prog.cpp:26:34: error: expected primary-expression before ‘int’
 void solve(TreeNode *n, int now, int dir){
                                  ^~~
prog.cpp:46:21: error: ‘TreeNode’ has not been declared
   int longestZigZag(TreeNode* root){
                     ^~~~~~~~
prog.cpp: In member function ‘int Solution::longestZigZag(int*)’:
prog.cpp:48:5: error: ‘solve’ was not declared in this scope
     solve(root, 0, -1);
     ^~~~~
stdout
Standard output is empty