Finding the common ancestor in a binary tree

后端 未结 10 1061
悲哀的现实
悲哀的现实 2021-02-09 02:50

This question was asked to me in an interview: I have a binary tree and I have to find the common ancestor (parent) given two random nodes of that tree. I am also given a point

10条回答
  •  死守一世寂寞
    2021-02-09 03:13

    1. Pre order traversal unless any 1 of the node is met and save the nodes visited uptil now.

    2. Inorder traversal,start saving the nodes when any 1 (of the two provided nodes) node is met,and save the list until the next node is met.

    3. post order traversal,start saving the nodes when both the nodes hav been visited...
                   A         
          B                  C         
      D       E          F       G       
    H   I   J   K      L   M   N   O     

    Suppose H and E are two random nodes.

    1. ABDH
    2. HDIBJE
    3. EBLMENOGCA

    Find the first node common in all three...

提交回复
热议问题