Lintcode 3715 · Lowest Common Ancestor V (最小祖先好题)

3715 · Lowest Common Ancestor VPRE

Algorithms

Medium

This topic is a pre-release topic. If you encounter any problems, please contact us via "Problem Correction", and we will upgrade your account to VIP as a thank you.

Description

Given a binary tree with a root node root and an array nodes of objects of class TreeNode, you need to find the Lowest Common Ancestor (LCA, Lowest Common Ancestor) of all nodes in nodes and return it.

Where all the nodes in nodes exist in that binary tree and all the nodes in the binary tree have different values from each other.

Only $39.9 for the "Twitter Comment System Project Practice" within a limited time of 7 days!

WeChat Notes Twitter for more information(WeChat ID jiuzhang15)

The number of nodes in the tree is in the range

1 , 1 0 4

1,10 4

1

0

9

.

1

0

9

−10

9

≤TreeNode.val≤10

9

All TreeNode.val are unique

All nodes[i] are unique

All nodes[i] exist in the tree

Example

Example 1

Input

root = {3,5,1,6,2,0,8,#,#,7,4}

nodes = [4,7]

Output

2

Explanation

The lowest common ancestor of TreeNode(4) and TreeNode(7) is TreeNode(2)

Example 2

Input

root = {3,5,1,6,2,0,8,#,#,7,4}

nodes = [2]

Output

2

Explanation

The lowest common ancestor of a single node is the node itself

Example 3

Input

root = {3,5,1,6,2,0,8,#,#,7,4}

nodes = [7,2,6,4]

Output

5

Explanation

The lowest common ancestor of TreeNode(7)、TreeNode(2)、TreeNode(6) and TreeNode(4) is TreeNode(5)

Example 4

Input

root = {3,5,1,6,2,0,8,#,#,7,4}

nodes = [0,2,3,1,5,8,6,4,7]

Output

3

Explanation

nodes contains all the nodes in the tree, and the lowest common ancestor of all the nodes in the tree is the root node

Tags

Related Problems

474

Lowest Common Ancestor II

Easy

578

Lowest Common Ancestor III

Medium

3715

Lowest Common Ancestor V

Medium

解法1:套用的labuladong的模板。

cpp 复制代码
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: The root node of a binary tree.
     * @param nodes: An array of objects of class TreeNode.
     * @return: The lowest common ancestor of nodes.
     */
    TreeNode* lowestCommonAncestor(TreeNode *root, vector<TreeNode*> &nodes) {
        if (!root) return NULL;
        unordered_set<TreeNode *> nodeSet;
        for (auto pNode : nodes) nodeSet.insert(pNode);
        TreeNode *res = helper(root, nodeSet);
        return res;
    }
private:
    TreeNode* helper(TreeNode *root, unordered_set<TreeNode *> &nodeSet) {
        if (!root) return NULL;
        if (nodeSet.find(root) != nodeSet.end()) return root;
        TreeNode *left = NULL, *right = NULL;
        
        left = helper(root->left, nodeSet);
        right = helper(root->right, nodeSet);
        
        if (left && right) return root;
        return left ? left : right;
    }
};
相关推荐
多米Domi0119 分钟前
0x3f 第25天 黑马web (145-167)hot100链表
数据结构·python·算法·leetcode·链表
LYFlied10 分钟前
【每日算法】LeetCode 207. 课程表
算法·leetcode·职场和发展
sali-tec12 分钟前
C# 基于OpenCv的视觉工作流-章7-膨胀
图像处理·人工智能·opencv·算法·计算机视觉
叫我:松哥15 分钟前
基于机器学习的地震风险评估与可视化系统,采用Flask后端与Bootstrap前端,系统集成DBSCAN空间聚类算法与随机森林算法
前端·算法·机器学习·flask·bootstrap·echarts·聚类
一起养小猫16 分钟前
LeetCode100天Day12-删除重复项与删除重复项II
java·数据结构·算法·leetcode
小O的算法实验室22 分钟前
2023年IEEE TITS SCI2区TOP,增强遗传算法+分布式随机多无人机协同区域搜索路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
Allen_LVyingbo30 分钟前
病历生成与质控编码的工程化范式研究:从模型驱动到系统治理的范式转变
前端·javascript·算法·前端框架·知识图谱·健康医疗·easyui
一起努力啊~30 分钟前
算法刷题--螺旋矩阵II+区间和+开发商购买土地
数据结构·算法·leetcode
Swift社区31 分钟前
LeetCode 470 用 Rand7() 实现 Rand10()
算法·leetcode·职场和发展
闻缺陷则喜何志丹33 分钟前
【图论 DFS 换根法】3772. 子图的最大得分|2235
c++·算法·深度优先·力扣·图论·换根法