-
Notifications
You must be signed in to change notification settings - Fork 31
/
1448-count-good-nodes-in-binary-tree.cpp
65 lines (54 loc) · 2.49 KB
/
1448-count-good-nodes-in-binary-tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem: LeetCode 1448 - Count Good Nodes in Binary Tree
Description:
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.
Intuition:
To count the number of good nodes in a binary tree, we can perform a depth-first search (DFS) and keep track of the maximum value seen so far.
For each node, if its value is greater than or equal to the maximum value seen so far, we increment the count of good nodes.
Approach:
1. Create a helper function, `countGoodNodesHelper`, to perform the DFS traversal and count the good nodes.
2. Initialize a count variable to keep track of the number of good nodes.
3. Start the DFS traversal from the root node with the initial maximum value as negative infinity.
4. In the `countGoodNodesHelper` function:
- Check if the current node is nullptr. If so, return.
- Update the maximum value seen so far to be the maximum of the current node's value and the current maximum value.
- If the current node's value is greater than or equal to the maximum value seen so far, increment the count of good nodes.
- Recursively call the `countGoodNodesHelper` function for the left and right children of the current node.
5. Return the count of good nodes.
Time Complexity:
The time complexity of the approach is O(n), where n is the number of nodes in the binary tree. We visit each node once during the DFS traversal.
Space Complexity:
The space complexity is O(h), where h is the height of the binary tree. This is the space used by the recursive call stack.
*/
/**
* 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 countGoodNodesHelper(root, INT_MIN);
}
private:
int countGoodNodesHelper(TreeNode *node, int maxSoFar) {
if (node == nullptr) {
return 0;
}
int count = 0;
if (node->val >= maxSoFar) {
count++;
}
int newMax = max(node->val, maxSoFar);
count += countGoodNodesHelper(node->left, newMax);
count += countGoodNodesHelper(node->right, newMax);
return count;
}
};