Longest ZigZag Path in a Binary Tree LeetCode solution in C++
1class Solution {2public:3int maxLength = 0;45void solve(TreeNode *root, int dir, int currLength) {6if (!root) return;7maxLength = max(maxLength, currLength);8solve(root->left, 0, dir ? currLength + 1 : 1);9solve(root->right, 1, dir ? 1 : currLength + 1);10}1112int longestZigZag(TreeNode *root) {13solve(root, 0, 0);14solve(root, 1, 0);15return maxLength;16}17};