-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1372.cpp
More file actions
30 lines (30 loc) · 926 Bytes
/
1372.cpp
File metadata and controls
30 lines (30 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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 {
public:
// res is the max zigzag path
int res = 0;
// left and right path of current node
pair<int, int> dfs(TreeNode* node) {
if (node == nullptr) return make_pair(-1, -1);
pair<int, int> left = dfs(node->left);
pair<int, int> right = dfs(node->right);
pair<int, int> out = make_pair(left.second + 1, right.first + 1);
res = max(res, out.first);
res = max(res, out.second);
return out;
}
int longestZigZag(TreeNode* root) {
dfs(root);
return res;
}
};