返回介绍

solution / 1600-1699 / 1676.Lowest Common Ancestor of a Binary Tree IV / README_EN

发布于 2024-06-17 01:03:15 字数 5991 浏览 0 评论 0 收藏 0

1676. Lowest Common Ancestor of a Binary Tree IV

中文文档

Description

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.val are unique.
  • All nodes[i] will exist in the tree.
  • All nodes[i] are distinct.

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, x):
#     self.val = x
#     self.left = None
#     self.right = None


class Solution:
  def lowestCommonAncestor(
    self, root: 'TreeNode', nodes: 'List[TreeNode]'
  ) -> 'TreeNode':
    def dfs(root):
      if root is None or root.val in s:
        return root
      left, right = dfs(root.left), dfs(root.right)
      if left and right:
        return root
      return left or right

    s = {node.val for node in nodes}
    return dfs(root)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
 */
class Solution {
  private Set<Integer> s = new HashSet<>();

  public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
    for (TreeNode node : nodes) {
      s.add(node.val);
    }
    return dfs(root);
  }

  private TreeNode dfs(TreeNode root) {
    if (root == null || s.contains(root.val)) {
      return root;
    }
    TreeNode left = dfs(root.left);
    TreeNode right = dfs(root.right);
    if (left == null) {
      return right;
    }
    if (right == null) {
      return left;
    }
    return root;
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
    unordered_set<int> s;
    for (auto node : nodes) s.insert(node->val);
    function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
      if (!root || s.count(root->val)) return root;
      auto left = dfs(root->left);
      auto right = dfs(root->right);
      if (!left) return right;
      if (!right) return left;
      return root;
    };
    return dfs(root);
  }
};
/**
 * 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 s = new Set();
  for (const node of nodes) {
    s.add(node.val);
  }
  function dfs(root) {
    if (!root || s.has(root.val)) {
      return root;
    }
    const [left, right] = [dfs(root.left), dfs(root.right)];
    if (left && right) {
      return root;
    }
    return left || right;
  }
  return dfs(root);
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文