Merge Two Balanced Binary Search Trees

Data Structures and Algorithms Note

Allie Hsu
Coder Life
2 min readFeb 2, 2022

--

Create a balanced BST by input

Get an array that uses inorder traversal for the tree

After getting the arrays from T1 and T2, merge two sorted array

Now, we use the merged sorted array to transfer into balanced BST

result

Time Complexity: O(m+n)

  • Do inorder traversal of the first tree and store into one array takes O(m) time.
  • Do inorder traversal of the second tree and store into another array takes O(n) time.
  • Merge the two sorted arrays into one array of size m + n takes O(m+n) time.
  • The function sorted_arr_to_bst for size m + n array also takes O(m+n) time.

Extra Tips

Get the size of the tree

--

--

Allie Hsu
Coder Life

A tech enthusiast who is keen to develop new skills