diff --git a/c/1448-Count-Good-Nodes-in-Binary-Tree.c b/c/1448-Count-Good-Nodes-in-Binary-Tree.c new file mode 100644 index 000000000..8d6cd8a03 --- /dev/null +++ b/c/1448-Count-Good-Nodes-in-Binary-Tree.c @@ -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); +}