50
/ \
40 60
/ \ / \
30 45 55 65
/ \
20 35
For example, find the least common ancestor (LCA) of 20 and 45 in the above tree. The root node, 50, is not between 20 and 45. We traverse left because both target nodes are less than 50. Now since 40 is between 20 and 45, this is the LCA.
No comments:
Post a Comment