# How to find the nearest common ancestors of two or more nodes?

Active3 hr before
Viewed126 times

common
90%

Here's a pure JavaScript version that is a little more efficient.,did not liked any of the answers above(want pure javascript and one function). that worked perfectly for me,efficient and also easier to understand:,handle edge-cases: node1 == node2 and node1.contains(node2),if parent of node_a contains node_b return parent (in our code, the parent is referenced as node_a)

Here's a pure JavaScript version that is a little more efficient.

```function parents(node) {
var nodes = [node]
for (; node; node = node.parentNode) {
nodes.unshift(node)
}
return nodes
}

function commonAncestor(node1, node2) {
var parents1 = parents(node1)
var parents2 = parents(node2)

if (parents1[0] != parents2[0]) throw "No common ancestor!"

for (var i = 0; i < parents1.length; i++) {
if (parents1[i] != parents2[i]) return parents1[i - 1]
}
}```
88%

Following is the implementation of the above algorithm.,Print path from root to a given node in a binary tree,Given a binary tree (not a binary search tree) and two values say n1 and n2, write a program to find the least common ancestor.,Print root to leaf paths without using recursion

72%

Given a binary tree and two nodes, x and y, find the lowest common ancestor (LCA) of x and y in it.,  For example, consider the following binary tree. Let x = 6 and y = 7. The common ancestors of nodes x and y are 1 and 3. Out of nodes 1 and 3, the LCA is 3 as it is farthest from the root.,The lowest common ancestor (LCA) of two nodes x and y in a binary tree is the lowest (i.e., deepest) node that has both x and y as descendants, where each node can be a descendant of itself (so if x is reachable from w, w is the LCA). In other words, the LCA of x and y is the shared ancestor of x and y that is located farthest from the root.,The time complexity of the above solution is O(n), where n is the total number of nodes in the binary tree. The program requires O(h) extra space for the call stack, where h is the height of the tree.

Given a binary tree and two nodes, `x` and `y`, find the lowest common ancestor (LCA) of `x` and `y` in it.

`x`

Given a binary tree and two nodes, `x` and `y`, find the lowest common ancestor (LCA) of `x` and `y` in it.

`y`

Given a binary tree and two nodes, `x` and `y`, find the lowest common ancestor (LCA) of `x` and `y` in it.

`x`

Given a binary tree and two nodes, `x` and `y`, find the lowest common ancestor (LCA) of `x` and `y` in it.

`y`
65%

For two nodes, a and b, the lowest common ancestor c is the lowest node in the binary tree that has a and b as its descendants.,If the current node has node a in its left subtree and b in its right subtree or vice versa then the current node will be the lowest common ancestor.,Two nodes may have more than one common ancestor, however, they can have only one lowest common ancestor,Below we have defined a binary tree in C++ and python that has the same structure as the illustration above.

75%

Users selects two or more elements in a HTML page. What i want to accomplish is to find those elements' common ancestors (so body node would be the common ancestor if none found before) ?,The commonAncestorContainer property of the he Range API mentioned above, alongside its selectNode, makes this a no-brainer.,This should be, more or less, as efficient as possible since it is pure DOM and has only one loop.,Taking another look at the question, I noticed the "or more" part of the "two or more" requirement had gone ignored by the answers. So I decided to tweak mine slightly to allow any number of nodes to be specified:

Try this:

```function get_common_ancestor(a, b) {
\$parentsa = \$(a).parents();
\$parentsb = \$(b).parents();

var found = null;

\$parentsa.each(function() {
var thisa = this;

\$parentsb.each(function() {
if (thisa == this) {
found = this;
return false;
}
});

if (found) return false;
});

return found;
}```

Use it like this:

`var el = get_common_ancestor("#id_of_one_element", "#id_of_another_element");`
40%

Let’s take a look at an example:,It is the lowest level parent shared by two nodes within a tree.,But the shared parent at the lowest level is 9, and it is referred to as the lowest common ancestor(LCA).,In this new implementation of the tree, we have stored an additional field, parent, in the node to store the parent node.

Below is an implementation of it:

```from collections
import dequeclass Node: def __init__(self, value, left = None, right = None): self.value = value self.left = left self.right = rightdef create_bt(value_queue): ""
"create binary tree"
""
if len(value_queue) <= 0: return None, None root = Node(value_queue.popleft()) current_queue = deque() current_queue.append(root) while len(current_queue) > 0 and len(value_queue) > 0: current_node = current_queue.popleft() left = value_queue.popleft() if left is not None: current_node.left = Node(left) current_queue.append(current_node.left) right = value_queue.popleft() if right is not None: current_node.right = Node(right) current_queue.append(current_node.right) return rootdef create_bt_fls(value_list): ""
"create binary create from list"
""
return create_bt(deque(value_list)) def lca_bst(bst, v1, v2): ""
"lowest common ancestor of two nodes       in a binary search tree"
""
if v1 <= bst.value <= v2 or v1 >= bst.value >= v2: return bst elif bst.value > v1 and bst.value > v2: return lca_bst(bst.left, v1, v2)
else :return lca_bst(bst.right, v1, v2) bt1 = create_bt_fls([5, 3, 7, 1, 4, 6, 9, 0, 2, None, None, None, None, 8, 10]) lca = lca_bst(bt1, 8, 10) print(lca.value)
''
'output:9'
''```