Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can merge two binary trees

I have two binary trees and I want to merge them. My first question is that whether we can merge two binary trees and if yes how efficiently I can perform the merge operations and what are the various ways i can perform the merging operations. ..?

like image 665
saurabh ranu Avatar asked Aug 22 '11 19:08

saurabh ranu


Video Answer


2 Answers

1) Flatten both the trees in to sorted lists.
2) Merge the lists from what you got in 1)
3) Construct the tree out of what you got in 2)

like image 168
hari Avatar answered Oct 21 '22 21:10

hari


This algorithm might help you.

like image 27
Dan W Avatar answered Oct 21 '22 22:10

Dan W