Skip to content

Commit 7c71ac4

Browse files
Add 1448 in c language
1 parent 825a25b commit 7c71ac4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
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.
3+
Return the number of good nodes in the binary tree.
4+
Time: O(n)
5+
Space: O(log(h)) Where h is the height of the tree
6+
*/
7+
8+
int nbGood(struct TreeNode* root, int m) {
9+
if (root==NULL)
10+
return 0;
11+
if (root->val >= m)
12+
return 1+nbGood(root->left, root->val)+nbGood(root->right, root->val);
13+
return nbGood(root->left, m)+nbGood(root->right, m);
14+
}
15+
16+
int goodNodes(struct TreeNode* root){
17+
return nbGood(root, INT_MIN);
18+
}

0 commit comments

Comments
 (0)