How to find the lowest common ancestor of two nodes in any binary tree?

The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as – struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort – Consider this binary tree : The inorder … Read more

What’s “P=NP?”, and why is it such a famous question? [closed]

Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it’s on-topic for Stack Overflow. Closed 9 years ago. The community reviewed whether to reopen this question 5 months ago and left it closed: Original close reason(s) were not resolved Improve this question The question … Read more

Computational complexity of Fibonacci Sequence

I understand Big-O notation, but I don’t know how to calculate it for many functions. In particular, I’ve been trying to figure out the computational complexity of the naive version of the Fibonacci sequence: int Fibonacci(int n) { if (n <= 1) return n; else return Fibonacci(n – 1) + Fibonacci(n – 2); } What … Read more

How can building a heap be O(n) time complexity?

Can someone help explain how can building a heap be O(n) complexity? Inserting an item into a heap is O(log n), and the insert is repeated n/2 times (the remainder are leaves, and can’t violate the heap property). So, this means the complexity should be O(n log n), I would think. In other words, for … Read more