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;
    }
};
相关推荐
Aileen_0v020 分钟前
【AI驱动的数据结构:包装类的艺术与科学】
linux·数据结构·人工智能·笔记·网络协议·tcp/ip·whisper
是小胡嘛21 分钟前
数据结构之旅:红黑树如何驱动 Set 和 Map
数据结构·算法
m0_7482550224 分钟前
前端常用算法集合
前端·算法
呆呆的猫1 小时前
【LeetCode】227、基本计算器 II
算法·leetcode·职场和发展
Tisfy1 小时前
LeetCode 1705.吃苹果的最大数目:贪心(优先队列) - 清晰题解
算法·leetcode·优先队列·贪心·
余额不足121381 小时前
C语言基础十六:枚举、c语言中文件的读写操作
linux·c语言·算法
yuanManGan2 小时前
数据结构漫游记:静态链表的实现(CPP)
数据结构·链表
火星机器人life3 小时前
基于ceres优化的3d激光雷达开源算法
算法·3d
虽千万人 吾往矣4 小时前
golang LeetCode 热题 100(动态规划)-更新中
算法·leetcode·动态规划
arnold664 小时前
华为OD E卷(100分)34-转盘寿司
算法·华为od