Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- int ans;
- public:
- int _longestZigZag(TreeNode *root, bool dir) {
- if (!root) {
- return -1;
- }
- int left = _longestZigZag(root->left, false);
- int right = _longestZigZag(root->right, true);
- ans = max(ans, 1 + max(left, right));
- return (dir ? left : right) + 1;
- }
- int longestZigZag(TreeNode* root) {
- ans = 0;
- _longestZigZag(root, false);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement