Skip to content

Commit

Permalink
이슈 #114에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 17, 2024
1 parent 1ea199e commit 694ef4f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions LeetCode/Count_Good_Nodes_in_Binary_Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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:
int goodNodes(TreeNode* root) {
return countGoodNodes(root, root->val);
}

private:
int countGoodNodes(TreeNode* node, int maxSoFar) {
if (node == nullptr) {
return 0;
}

int goodNodeCount = (node->val >= maxSoFar) ? 1 : 0;

maxSoFar = max(maxSoFar, node->val);

goodNodeCount += countGoodNodes(node->left, maxSoFar);
goodNodeCount += countGoodNodes(node->right, maxSoFar);

return goodNodeCount;
}
};

0 comments on commit 694ef4f

Please sign in to comment.