Added a improved time complexity version. O(n) is inevitible for each node.