Given two nodes p and q from a tree, return their lowest common ancestor.
Given two nodes p and q from a tree, return their lowest common ancestor.
Lowest Common Ancestor | ntree.ai ntree.ai ntree.ai Lowest Common Ancestor Visualise Run Submit Description Solutions Submissions Lowest Common Ancestor medium solved attempted Topics Given two nodes p and q from a tree, return their lowest common ancestor. Each node has a reference to its parent node. A node counts as a descendant of itself, so if one of p and q is an ancestor of the other, that ancestor is the answer. Example 1: 3 5 6 2 7 4 1 0 8 Input: tree = [3, null, 5, 1, null, 6, 2, null, 0, 8, null, null, 7, 4], p = 4, q = 6 Output: 5 Example 2: 3 5 6 2 7 4 1 0 8 Input: tree = [3, null
Given two nodes p and q from a tree, return their lowest common ancestor.
Lowest Common Ancestor | ntree.ai ntree.ai ntree.ai Lowest Common Ancestor Visualise Run Submit Description Solutions Submissions Lowest Common Ancestor medium solved attempted Topics Given two nodes p and q from a tree, return their lowest common ancestor. Each node has a reference to its parent node. A node counts as a descendant of itself, so if one of p and q is an ancestor of the other, that ancestor is the answer. Example 1: 3 5 6 2 7 4 1 0 8 Input: tree = [3, null, 5, 1, null, 6, 2, null, 0, 8, null, null, 7, 4], p = 4, q = 6 Output: 5 Example 2: 3 5 6 2 7 4 1 0 8 Input: tree = [3, null