Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions c/1448-Count-Good-Nodes-in-Binary-Tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
Return the number of good nodes in the binary tree.
Time: O(n)
Space: O(log(h)) Where h is the height of the tree
*/

int nbGood(struct TreeNode* root, int m) {
if (root==NULL)
return 0;
if (root->val >= m)
return 1+nbGood(root->left, root->val)+nbGood(root->right, root->val);
return nbGood(root->left, m)+nbGood(root->right, m);
}

int goodNodes(struct TreeNode* root){
return nbGood(root, INT_MIN);
}