当前位置: 首页 > news >正文

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的模板。

/*** 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;}
};
http://www.lryc.cn/news/213028.html

相关文章:

  • SQL LIKE 运算符
  • AR眼镜定制开发-智能眼镜的主板硬件、软件
  • [双指针] (三) LeetCode LCR 179. 查找总价格为目标值的两个商品 和 15. 三数之和
  • 左移测试,如何确保安全合规还能实现高度自动化?
  • mysql 增删改查基础命令
  • C# 使用 AES 加解密文件
  • SSM培训报名管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目
  • 锁表后引发的几种删除方式与不同的扩展
  • 20.2 OpenSSL 非对称RSA加解密算法
  • MySQL安装『适用于 CentOS 7』
  • 国家数据局成立,公共数据如何掘金?
  • PostgreSQL基于Patroni方案的高可用启动流程分析
  • opencv+yolov8实现监控画面报警功能
  • 基于深度学习的单图像人群计数研究:网络设计、损失函数和监控信号
  • C++递归实现验证⼆叉搜索树
  • ♥ uniapp 环境搭建
  • 京东商品链接获取京东商品评论数据(用 Python实现京东商品评论信息抓取),京东商品评论API接口,京东API接口
  • docker容器中安装ROS1/ROS2(不用配任何环境,10分钟搞定)
  • 如何解决ssh登录报错WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  • Mysql5.7安装配置详细图文教程(msi版本)
  • 运行dl4j-examples的主要一些依赖
  • PSRAM伪静态RAM芯片APS6404L
  • 低级语言汇编真的各个面不如汇编吗?
  • PyG edge index 转换回 邻接矩阵
  • JavaSE19——file文件类
  • mongodb记录
  • Go语言:数组和切片
  • OPENCV 闭运算实验示例代码morphologyEx()函数
  • UE4 体积云制作 学习笔记
  • visual studio编译QtAV