-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 188 ms (36.24%), Space: 94.3 MB (63.41%) - LeetHub
- Loading branch information
1 parent
944754a
commit cbb2a12
Showing
1 changed file
with
17 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
1372-longest-zigzag-path-in-a-binary-tree/1372-longest-zigzag-path-in-a-binary-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public: | ||
int mx=0; | ||
int solve(TreeNode* root,bool flag) | ||
{ | ||
if(root==NULL)return 0; | ||
int left=1+solve(root->left,true); | ||
int right=1+solve(root->right,false); | ||
mx=max(mx,max(left,right)); | ||
return flag==true?right:left; | ||
|
||
} | ||
int longestZigZag(TreeNode* root) { | ||
solve(root,true); | ||
return mx-1; | ||
} | ||
}; |