0

How will you rebuild a given BST into AVL which contains exactly the same keys? The algorithm running time should be O(n) and its allowed to use O(n) additional space. Any ideas? The whole pseudo-code is not necessary, any idea or suggestion would be appreciated! Thanks!

dvainrub
  • 133
  • 2
  • 6
  • 1
    Are you asking how to turn a potentially unbalanced binary tree into a balanced (AVL) tree? Have you done any research on this at all? – Jim Mischel Aug 27 '13 at 19:37
  • Find each unbalanced node and balance it. Ya that is the simplest way. Start from leaves and then moved up. – Aditya Aug 28 '13 at 00:13

1 Answers1

2
  1. Extract all keys to sorted array (O(n) space) with suitable traversal method (O(n) time)
  2. Build perfectly balanced tree from sorted array (O(n) time) (simultaneously filling AVL balance factors for all nodes)

I 've omitted the details for your own research

MBo
  • 77,366
  • 5
  • 53
  • 86