Skip to content

Instantly share code, notes, and snippets.

@epalaz
Last active April 19, 2021 17:48
Show Gist options
  • Save epalaz/7d0e8f57125253e523f59bd45272b626 to your computer and use it in GitHub Desktop.
Save epalaz/7d0e8f57125253e523f59bd45272b626 to your computer and use it in GitHub Desktop.
Interview Questions

Lowest Common Ancestor in a Binary Tree

How can you find the lowest common ancestor of 2 given nodes in a binary tree data structure.

Sample Tree

Node:

  • val: int
  • right: Node
  • left: Node

Write a function findLCA(Node node1, Node node2) -> Node

findLCA(Node(7), Node(4)) -> Node(2) findLCA(Node(7), Node(6)) -> Node(5)

Extra

How can this be improved if tree is a Binary Search Tree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment