Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- O(n)
- countAllInRange(7,10)
- Function int countAllInRange(k1,k2, tree){
- If (tree == null)
- Return 0;
- Else
- If(tree.node.value >= k1 && tree.node.value <= k2)
- Return 1+ countAllInRange(k1,k2, tree->left)
- + countAllInRange(k1,k2, tree->right)
- Else
- If(tree.node.value < k1)
- Return countAllInRange(k1,k2, tree->right)
- If(tree.node.value > k2)
- Return countAllInRange(k1,k2, tree->left)
- Return countAllInRange(k1,k2, tree->left)
- + countAllInRange(k1,k2, tree->right)
- Foreach(node in tree)
- If(node.value >= k1 && node.value <= k2)
- countNodesInRange++;
- return countNodesInRange
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement