Longest ZigZag Path in a Binary Tree LeetCode solution in C++

1
class Solution {
2
public:
3
int maxLength = 0;
4

5
void solve(TreeNode *root, int dir, int currLength) {
6
if (!root) return;
7
maxLength = max(maxLength, currLength);
8
solve(root->left, 0, dir ? currLength + 1 : 1);
9
solve(root->right, 1, dir ? 1 : currLength + 1);
10
}
11

12
int longestZigZag(TreeNode *root) {
13
solve(root, 0, 0);
14
solve(root, 1, 0);
15
return maxLength;
16
}
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0