質問

Consider the code :

int Tree::height(tree *node) {
    if (!node) return -1;

    return 1 + max(height(node->left), height(node->right));
}

What is the complexity of this method ? O(n) or O(log(n)) ?

I think it's O(n) , since in the worst case we'd check all the nodes , doesn't ?

Can you give an example of a better implementation ?

Thanks

役に立ちましたか?

解決

I think you're checking all the nodes in all cases, not just the worst case. So yes, O(N).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top