-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1676-lowest-common-ancestor-of-a-binary-tree-iv.js
More file actions
44 lines (38 loc) · 1.33 KB
/
1676-lowest-common-ancestor-of-a-binary-tree-iv.js
File metadata and controls
44 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 1676. Lowest Common Ancestor of a Binary Tree IV
* https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/
* Difficulty: Medium
*
* Given the root of a binary tree and an array of TreeNode objects nodes, return the lowest common
* ancestor (LCA) of all the nodes in nodes. All the nodes will exist in the tree, and all values
* of the tree's nodes are unique.
*
* Extending the definition of LCA on Wikipedia: "The lowest common ancestor of n nodes p1,
* p2, ..., pn in a binary tree T is the lowest node that has every pi as a descendant (where we
* allow a node to be a descendant of itself) for every valid i". A descendant of a node x is a
* node y that is on the path from node x to some leaf node.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode[]} nodes
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, nodes) {
const targetSet = new Set(nodes);
return helper(root);
function helper(node) {
if (!node) return null;
if (targetSet.has(node)) return node;
const leftResult = helper(node.left);
const rightResult = helper(node.right);
if (leftResult && rightResult) return node;
return leftResult || rightResult;
}
};