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.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [4,7] Output: 2 Explanation: The lowest common ancestor of nodes 4 and 7 is node 2.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1] Output: 1 Explanation: The lowest common ancestor of a single node is the node itself.
Example 3:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [7,6,2,4] Output: 5 Explanation: The lowest common ancestor of the nodes 7, 6, 2, and 4 is node 5.
Constraints:
- The number of nodes in the tree is in the range
[1, 104]. -109 <= Node.val <= 109- All
Node.valare unique. - All
nodes[i]will exist in the tree. - All
nodes[i]are distinct.
Companies:
Microsoft
Related Topics:
Tree, Depth-First Search, Binary Tree
Similar Questions:
- Lowest Common Ancestor of a Binary Search Tree (Easy)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Lowest Common Ancestor of Deepest Leaves (Medium)
- Lowest Common Ancestor of a Binary Tree II (Medium)
- Lowest Common Ancestor of a Binary Tree III (Medium)
- Lowest Common Ancestor of a Binary Tree IV (Medium)
// OJ: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iv/
// Author: github.com/lzl124631x
// Time: O(N + T) where `N` is the number of nodes in the tree, `T` is the size of `nodes`, and `H` is the height of the tree.
// Space: O(H + T)
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
unordered_set<TreeNode*> s(begin(nodes), end(nodes));
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode *node) -> TreeNode*{
if (!node) return nullptr;
if (s.count(node)) return node;
auto left = dfs(node->left), right = dfs(node->right);
return !left ? right : (!right ? left : node);
};
return dfs(root);
}
};