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=0;
- public:
- int goodNodes(TreeNode* root) {
- dfs(root, INT_MIN);
- return ans;
- }
- void dfs(TreeNode* curr, int maxSoFar){
- if(!curr) return;
- if(maxSoFar <= curr->val) ans++;
- dfs(curr->left, max(maxSoFar, curr->val));
- dfs(curr->right, max(maxSoFar, curr->val));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment