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;
    }
};
相关推荐
浅念同学9 分钟前
算法.图论-并查集上
java·算法·图论
何不遗憾呢18 分钟前
每日刷题(算法)
算法
立志成为coding大牛的菜鸟.22 分钟前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞22 分钟前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
liangbm328 分钟前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
潮汐退涨月冷风霜34 分钟前
机器学习之非监督学习(四)K-means 聚类算法
学习·算法·机器学习
B站计算机毕业设计超人40 分钟前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~44 分钟前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
ahauedu1 小时前
案例分析-Stream List 中取出值最大的前 5 个和最小的 5 个值
数据结构·list
X同学的开始3 小时前
数据结构之二叉树遍历
数据结构