Input: root =[7,6,5,4,3,2,1], k =2Output: 3Explanation: Number the nodes from 1 to 7.The values in the subtree of node 1:{1,2,3,4,5,6,7}. Since node.val==7, there are 6 nodes having a smaller value than its value. So it's great enough.The values in the subtree of node 2:{3,4,6}. Since node.val==6, there are 2 nodes having a smaller value than its value. So it's great enough.The values in the subtree of node 3:{1,2,5}. Since node.val==5, there are 2 nodes having a smaller value than its value. So it's great enough.It can be shown that other nodes are not great enough.See the picture below for a better understanding.
graph TD;
G(7) --- F(6) & E(5)
F --- D(4) & C(3)
E --- B(2) & A(1)
Example 2:
1
2
3
4
5
6
7
Input: root =[1,2,3], k =1Output: 0Explanation: Number the nodes from 1 to 3.The values in the subtree of node 1:{1,2,3}. Since node.val==1, there are no nodes having a smaller value than its value. So it's not great enough.The values in the subtree of node 2:{2}. Since node.val==2, there are no nodes having a smaller value than its value. So it's not great enough.The values in the subtree of node 3:{3}. Since node.val==3, there are no nodes having a smaller value than its value. So it's not great enough.See the picture below for a better understanding.
graph TD;
A(1) --- B(2) & C(3)
Example 3:
1
2
3
4
5
6
7
Input: root =[3,2,2], k =2Output: 1Explanation: Number the nodes from 1 to 3.The values in the subtree of node 1:{2,2,3}. Since node.val==3, there are 2 nodes having a smaller value than its value. So it's great enough.The values in the subtree of node 2:{2}. Since node.val==2, there are no nodes having a smaller value than its value. So it's not great enough.The values in the subtree of node 3:{2}. Since node.val==2, there are no nodes having a smaller value than its value. So it's not great enough.See the picture below for a better understanding.
graph TD;
C(3) --- B(2) & B1(2)
Constraints:
The number of nodes in the tree is in the range [1, 10^4].
How many nodes in its subtree have values less than the current node’s value.
We can use postorder DFS to compute the size and collect all values in the subtree, then use binary search (or counting) to efficiently count how many are less than the current node’s value.
structTreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
classSolution {
public:int ans =0, kk;
std::vector<int> dfs(TreeNode* node) {
if (!node) return {};
auto l = dfs(node->left);
auto r = dfs(node->right);
std::vector<int> merged;
merged.reserve(l.size() + r.size() +1);
std::merge(l.begin(), l.end(), r.begin(), r.end(), std::back_inserter(merged));
auto it = std::lower_bound(merged.begin(), merged.end(), node->val);
int less = it - merged.begin();
merged.insert(it, node->val);
int sz = merged.size();
if (sz >= kk && less >= kk) ans++;
return merged;
}
intcountGreatEnoughNodes(TreeNode* root, int k) {
ans =0; kk = k;
dfs(root);
return ans;
}
};
dataclassTreeNode(var `val`: Int, var left: TreeNode? = null, var right: TreeNode? = null)
classSolution {
var ans = 0var kk = 0funcountGreatEnoughNodes(root: TreeNode?, k: Int): Int {
ans = 0; kk = k
fundfs(node: TreeNode?): List<Int> {
if (node ==null) return emptyList()
val l = dfs(node.left)
val r = dfs(node.right)
val merged = mutableListOf<Int>()
var i = 0; var j = 0while (i < l.size && j < r.size) {
if (l[i] < r[j]) merged.add(l[i++])
else merged.add(r[j++])
}
while (i < l.size) merged.add(l[i++])
while (j < r.size) merged.add(r[j++])
val less = merged.indexOfFirst { it>= node.`val` }.let { if (it== -1) merged.size elseit }
merged.add(less, node.`val`)
if (merged.size >= kk && less >= kk) ans++return merged
}
dfs(root)
return ans
}
}