leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1372.cpp (399B)


      1 class Solution {
      2     int res = 0;
      3 
      4     int dfs(TreeNode *root, bool zig) {
      5         if (!root) return -1;
      6 
      7         int left = dfs(root->left, false);
      8         int right = dfs(root->right, true);
      9         res = max(res, max(left + 1, right + 1));
     10 
     11         return zig ? left + 1 : right + 1;
     12     }
     13 
     14   public:
     15     int longestZigZag(TreeNode *root) {
     16         dfs(root, false);
     17         return res;
     18     }
     19 };